Dean
Dean

Reputation: 63

If Statement is not functional

Hello I am stuck with this code, I don't know whats wrong with my if statement but it keeps displaying T1T on every sheet. Please help. Thank you.

Public Sub CommandButton4_Click()

Dim Sheet As Worksheet
Dim i As Integer

For i = 2 To ActiveWorkbook.Worksheets.Count

  Set Sheet = ActiveWorkbook.Worksheets(i)

  If Sheet.Name = "USA" & i And Range("D20").Value = "Branch Codes" Then

    Sheet.Cells(2, 1).Value = "B1"

  Else

    Sheet.Cells(2, 1).Value = "T1T"

  End If

Next i

End Sub

Upvotes: 0

Views: 29

Answers (1)

Cohan
Cohan

Reputation: 4544

You will also want to set it to sheet.range("D20") if you want it to read from the given sheet. Otherwise, it will refer to D20 on the first sheet.

If Sheet.Name = "USA" & i And Sheet.Range("D20").Value = "Branch Codes" Then

Upvotes: 1

Related Questions