Cells.Find() 引发“运行时错误 91:对象变量或块未设置”

det*_*ail 5 excel vba

我想要什么: 我有很多带有不同设备的床单。让我们称这些工作表之一为“WS1”。

我有一个单独的表格,其中包含所有现有设备和旁边的相应操作系统。我们称之为“列表”。

现在我希望其他工作表(例如“WS1”)检查“列表”,找到正确的设备,并将正确的操作系统复制到 WS1 工作表中。

手动方式是:

  • 选择 WS1 的单元格“C3”并复制它。
  • 打开“列表”-工作表并找到复制的条目
  • 选择找到的条目左侧的单元格并复制它
  • 再次打开 WS1,选择活动单元格右侧的左侧单元格并粘贴新的剪贴板(其中包含操作系统)
  • 选择活动单元格下方和右侧的下一个单元格。
  • 循环直到 WS1 中的每个设备都装满了一个操作系统

到目前为止我所得到的:

Dim DataObj As New MSForms.DataObject
Dim strCliBoa As String
'strCliBoa = DataObj.GetText
DataObj.GetFromClipboard

Range("C3").Select
Selection.Copy
strCliBoa = DataObj.GetText
Sheets("list").Select
Range("A1").Select
Cells.Find(What:=strCliBoa, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Offset(0, -1).Select
Selection.Copy
strCliBoa = DataObj.GetText
Sheets("WS1").Select
ActiveCell.Offset(0, -1).Select
ActiveSheet.Paste
ActiveCell.Offset(1, 1).Select
Run Code Online (Sandbox Code Playgroud)

我的问题: “运行时错误 91:对象变量或块变量未设置”,它标记了 cell.find-method。

有人能告诉我我做错了什么吗?^^ 提前致谢!

(哦,差点忘了:我在 Win7 上使用的是 ms excel 2010)

Sob*_*gen 7

如果未找到您要查找的字符串,则会出现该错误。如果未找到任何内容,则 find 函数将返回“Nothing”

    Dim r As Range

    Set r = Cells.find(What:=strCliBoa, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=True, SearchFormat:=False)

    If r Is Nothing Then
        'handle error
    Else
        'fill in your code
    End If
Run Code Online (Sandbox Code Playgroud)