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

标签 excel 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 = "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/

相关文章:

excel - Excel 中用户定义的 IFS 函数的 VBA 代码

excel - 强制 COM 服务器保持打开状态

vb.net - Excel 进程未在 VB.net 中关闭

excel - 在同一个工作表中复制 10 行乘 10 行

c# - ClosedXML 是否支持设置工作表的缩放级别?

vba - 如何使用 VBA 删除 Excel 功能区中最近的文档历史记录

excel - 我需要设置excel工作表的路径,以便我可以使用相对路径加载dll

ms-access - 确定子窗体/子报表是否具有在 MS Access 中加载的窗体或报表

excel - 随着 vsto 和 excel 越来越多地尝试将 Visual Studio 和 C# 结合起来,VBA 死了吗?

Excel 宏 : how do I change all row heights, 但是如果 cell.value = bold 会使单元格高度变大?