vba在另一个工作表上查找单元格值并重命名值?

如何从活动工作表上的单元格中获取值并在非活动工作表上查找它,然后重命名该值?

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 =“testing”,但这会导致错误:对象variables或未设置variables的块

请有人告诉我我要去哪里错了?

坏消息是你不能在非活动的工作表上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属性 ,而其他工作表中的其他时间有点令人困惑。 我不是sute我在Range.Find方法中得到了What参数。

请参阅如何避免使用Excel中的selectVBAmacros来获取更多的方法来摆脱依靠select和activate来实现您的目标。