如果Statement不起作用

时间:2015-07-07 16:39:41

标签: vba excel-vba excel

你好,我对这段代码感到困惑,我不知道我的if语句有什么问题,但它会在每张表上显示T1T。请帮忙。谢谢。

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

1 个答案:

答案 0 :(得分:1)

如果您希望从给定的工作表中读取,您还需要将其设置为sheet.range("D20")。否则,它将在第一张纸上引用D20。

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