我正在其他工作簿中使用Personal.xlsb
(来自此thread)的下面宏。但这并没有突出显示任何行,没有错误,没有任何内容。在独立工作簿中运行时,它可以正常工作。
任何帮助将不胜感激。
Sub Highlighting()
Dim rw As Long
Dim lastrw As Long
' Define 2 different highlighting colours by their RGB values
Dim col1 As Long
Dim col2 As Long
col1 = RGB(255, 230, 180)
col2 = RGB(180, 230, 255)
' "Current" colour for highlighting
Dim col As Long
col = col1
With ThisWorkbook.ActiveSheet
' Get last row by last non-empty cell in column A
lastrw = .Cells(.Rows.Count, 1).End(xlUp).Row
' Cycle through column A
For rw = 1 To lastrw
' Highlight row with current colour
.Range("A" & rw & ":G" & rw).Interior.Color = col
' If column A value in next row is different, change colour
If .Cells(rw + 1, 1).Value <> .Cells(rw, 1) Then
' Set to whichever colour it is not
If col = col1 Then
col = col2
Else
col = col1
End If
End If
Next rw
End With
End Sub
答案 0 :(得分:1)
这是因为在宏中您使用的是ThisWorkbook.ActiveSheet
,因此该代码仅在您的Personal.xlsb
工作簿中有效。
替换行...
With ThisWorkbook.ActiveSheet
有
With ActiveWorkbook.ActiveSheet
该代码将在任何活动的工作簿上工作。
答案 1 :(得分:0)
更改
With ThisWorkbook.ActiveSheet
...到
With ActiveWorkbook.ActiveSheet
这将使您可以在当前活动的工作簿/工作表上使用它。 ThisWorkbook引用了代码所在的Personal.xlsb(例如 This Workbook)