为什么通过索引引用 sheet 仍然隐式引用 activesheet
Why is referencing sheet by index still implicity referencing referencing by activesheet
我有以下格式化 sheet 的代码。此工作簿中有多个 sheet,因此我只想对第一个进行操作。但是,如果我有任何 sheet 不是第一个激活的,例如第二个,代码会抛出 1004 错误。我不确定为什么要指定和定义目标工作sheet。
Option Explicit
Sub SelectByJobNumber()
Dim LastRow As Long
Dim OperatingRow As Variant
Dim CountOfMatching As Long
Dim WB As Worksheet
Set WB = ThisWorkbook.Worksheets(1)
LastRow = WB.Cells(ActiveSheet.Rows.Count, "C").End(xlUp).Row
WB.Range(Cells(LastRow + 2, 19), Cells(LastRow + 2, 22)).Formula = "=" & "Sum(S74:S" & LastRow & ")"
WB.Range(Cells(LastRow + 2, 19), Cells(LastRow + 2, 22)).NumberFormat = "$#,##0.00"
For OperatingRow = 74 To LastRow
If WB.Cells(OperatingRow, 3) = WB.Cells(OperatingRow + 1, 3) Then
CountOfMatching = CountOfMatching + 1
Else
WB.Range(Cells(OperatingRow - CountOfMatching, 3), Cells(OperatingRow, 21)).BorderAround ColorIndex:=1, Weight:=xlMedium
WB.Cells(OperatingRow - CountOfMatching, 22) = Application.Sum(Range(Cells(OperatingRow - CountOfMatching, 21), Cells(OperatingRow, 21)))
If WB.Cells(OperatingRow - CountOfMatching, 22) = 0 Then
WB.Cells(OperatingRow - CountOfMatching, 23) = "Text for Zero Total Payable"
Else
WB.Cells(OperatingRow - CountOfMatching, 23) = "Not Paid"
End If
WB.Cells(OperatingRow - CountOfMatching, 22).NumberFormat = "$#,##0.00"
CountOfMatching = 0
End If
Next OperatingRow
If WB.Cells(LastRow + 2, 21) = WB.Cells(LastRow + 2, 22) Then
WB.Range(Cells(LastRow + 2, 21), Cells(LastRow + 2, 22)).BorderAround ColorIndex:=4, Weight:=xlMedium
Else
WB.Range(Cells(LastRow + 2, 21), Cells(LastRow + 2, 22)).BorderAround ColorIndex:=3, Weight:=xlMedium
End If
End Sub
原因是在这样的代码中:
WB.Range(Cells(LastRow + 2, 19), Cells(LastRow + 2, 22))
对象 Cells
不合格,因此默认为 ActiveWorkbook.ActiveSheet.Cells()
。这感觉很傻,因为你已经限定了以 Cells()
作为参数的 Range()
,但这些参数是它们自己的对象,也必须被限定。
改为考虑:
With WB
.Range(.Cells(LastRow + 2, 19), .Cells(LastRow + 2, 22))
End With
或者你可以用 WB.
单独限定它们,如果这样感觉更干净的话。
我有以下格式化 sheet 的代码。此工作簿中有多个 sheet,因此我只想对第一个进行操作。但是,如果我有任何 sheet 不是第一个激活的,例如第二个,代码会抛出 1004 错误。我不确定为什么要指定和定义目标工作sheet。
Option Explicit
Sub SelectByJobNumber()
Dim LastRow As Long
Dim OperatingRow As Variant
Dim CountOfMatching As Long
Dim WB As Worksheet
Set WB = ThisWorkbook.Worksheets(1)
LastRow = WB.Cells(ActiveSheet.Rows.Count, "C").End(xlUp).Row
WB.Range(Cells(LastRow + 2, 19), Cells(LastRow + 2, 22)).Formula = "=" & "Sum(S74:S" & LastRow & ")"
WB.Range(Cells(LastRow + 2, 19), Cells(LastRow + 2, 22)).NumberFormat = "$#,##0.00"
For OperatingRow = 74 To LastRow
If WB.Cells(OperatingRow, 3) = WB.Cells(OperatingRow + 1, 3) Then
CountOfMatching = CountOfMatching + 1
Else
WB.Range(Cells(OperatingRow - CountOfMatching, 3), Cells(OperatingRow, 21)).BorderAround ColorIndex:=1, Weight:=xlMedium
WB.Cells(OperatingRow - CountOfMatching, 22) = Application.Sum(Range(Cells(OperatingRow - CountOfMatching, 21), Cells(OperatingRow, 21)))
If WB.Cells(OperatingRow - CountOfMatching, 22) = 0 Then
WB.Cells(OperatingRow - CountOfMatching, 23) = "Text for Zero Total Payable"
Else
WB.Cells(OperatingRow - CountOfMatching, 23) = "Not Paid"
End If
WB.Cells(OperatingRow - CountOfMatching, 22).NumberFormat = "$#,##0.00"
CountOfMatching = 0
End If
Next OperatingRow
If WB.Cells(LastRow + 2, 21) = WB.Cells(LastRow + 2, 22) Then
WB.Range(Cells(LastRow + 2, 21), Cells(LastRow + 2, 22)).BorderAround ColorIndex:=4, Weight:=xlMedium
Else
WB.Range(Cells(LastRow + 2, 21), Cells(LastRow + 2, 22)).BorderAround ColorIndex:=3, Weight:=xlMedium
End If
End Sub
原因是在这样的代码中:
WB.Range(Cells(LastRow + 2, 19), Cells(LastRow + 2, 22))
对象 Cells
不合格,因此默认为 ActiveWorkbook.ActiveSheet.Cells()
。这感觉很傻,因为你已经限定了以 Cells()
作为参数的 Range()
,但这些参数是它们自己的对象,也必须被限定。
改为考虑:
With WB
.Range(.Cells(LastRow + 2, 19), .Cells(LastRow + 2, 22))
End With
或者你可以用 WB.
单独限定它们,如果这样感觉更干净的话。