我有一个xlsm文件,其中包含一个单击按钮,单击该按钮时应打开单独的工作簿并搜索特定颜色的单元格的所有工作表。 问题是,它不是搜索其他工作簿的工作表,而只是搜索自己。我是VBA新手,感觉我已经围绕互联网6次试图解决这个问题。我在这里做错了什么?计算另一个工作簿中工作表的单元格
Private Sub CommandButton1_Click()
Dim wb As Workbook
Dim ws As Worksheet
Dim holdCount As Integer
Dim cellColour As Long
Dim cell As Range, rng As Range
Set wb = Workbooks.Open("blahblahblah.xls")
Set rng = Range("A1:A20")
holdCount = 0
cellColour = RGB(255, 153, 0)
For Each ws In wb.Worksheets
For Each cell In rng
If cell.Interior.Color = cellColour Then
holdCount = holdCount + 1
End If
Next cell
Next ws
MsgBox "found " & holdCount
End Sub
RNG不被重新分配在环。对于范围(ws.cells(1,1),ws.cells(20,1))中的每个单元格,将'rng'中的每个单元格更改为'' – Zerk
我试过了,但得到了“应用程序定义的或对象定义的错误“ – WaltVinegar