如何从事件工作表上的单元格中获取值并在非事件工作表上查找它然后重命名该值?
Dim rw As Long
rw = ActiveCell.Row
If Sheets("Home").Range("D" & rw).Value = "Tender" Then
With Worksheets("Time Allocation").Columns("B:B")
Set cell = .Find(What:=.Range("B" & rw).Value, After:=Range("B" & rw), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not cell Is Nothing Then
cell.Value = "test"
Else
cell.Value = "test"
End If
End With
End If
我曾尝试使用 cell.value = "test"但这会导致错误:
未设置变量的对象变量或 block
请问有人可以告诉我哪里出错了吗?
最佳答案
坏消息是你不能.Select
非事件工作表上的一个或多个单元格。好消息是绝对没有要求您这样做,实际上它通常比直接寻址单元格、单元格或列效率低。
Dim rw As Long, cell as range
rw = ActiveCell.Row
If Sheets("Sheet1").Range("D" & rw).Value = "Tender" Then
With Worksheets("Sheet2").Columns("B:B")
Set cell = .Find(What:=Sheets("Sheet1").Range("B" & rw).Value, LookIn:=xlFormulas, _
LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not cell Is Nothing Then
cell = "test" '<~~the default property is the .Value
Else
MsgBox "cannot test. not found. cell is nothing and cannot be referenced."
End If
End With
End If
您在两个工作表之间来回切换并引用 ActiveCell property 的方式就像有时在一个工作表上一样,有时在另一个工作表上有点令人困惑。我不是 sute 我得到了
What
Range.Find method 中的参数权.见 How to avoid using Select in Excel VBA macros了解更多摆脱依赖选择和激活来实现目标的方法。
关于excel - vba 在另一个工作表上查找单元格值并重命名值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32500106/