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

我想要的:我有很多不同的设备。 我们称之为“WS1”。

而且我有一个单独的工作表,其中包含所有现有的设备和相应的操作系统。 这一个我们称之为“名单”。

现在我想让其他工作表(例如“WS1”)检查“列表”,find合适的设备,并将正确的操作系统复制到WS1表单中。

手动的方式是:

  • selectWS1的单元格“C3”并复制它。
  • 打开“清单” – 表格并find复制的条目
  • select留在find的条目的单元格并复制它
  • 再次打开WS1,select活动单元格旁边的左侧单元格并粘贴新的剪贴板(其中包含操作系统)
  • select活动单元格右侧的下一个单元格。
  • 直到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 

我的问题: “运行时错误91:对象variables或块variables未设置”,它标记了cells.find方法。

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

(哦,几乎忘记了:我在Win7上使用ms excel 2010)

如果你找的string没有find,你会得到这个错误。 如果没有find,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 

我将使用VLOOKUP()函数为您提供答案。 所以Sheet1包含几个设备,我需要find正确的操作系统。 Sheet2包含设备和操作系统之间的匹配。

在Sheet1上,在设备旁边的单元格中input该公式,并将其拉下(当然也可以根据您的具体需要进行编辑)。

 =VLOOKUP(A2;Sheet2!$A$1:$B$20;2;0) 

编辑:只有操作系统在第二列VLOOKUP函数将工作。 围绕列切换或在末尾使用帮助列来包含操作系统。

在您有设备名称(WS1)的工作表中input公式:

=INDEX(List!$A$2:$B$10;MATCH('WS1'!C3;List!$B$2:$B$10;0);1)

其中:
List!$A$2:$B$10是您在列表中具有Devices + OS的范围
'WS1'!C3是您要在列表中search的设备(您的案例中的“WS1”)
List!$B$2:$B$10是图纸列表中列出设备的列。

编辑1 – VBA代码

如果你想使用VBA然后使用这个:

 Sub FindDevicePasteOS() 'Find corresponding OS for the device Dim intRow As Integer Dim wsht As Worksheet For Each wsht In Worksheets If wsht.Name <> "List" Then 'add more sheets you want to exclude using OR (eg ... Or wsht.Name <> "Cover Sheet" Then) For intRow = 3 To wsht.Cells(Rows.Count, 3).End(xlUp).Row 'presuming there is nothing else in the column C below the devices If Not Worksheets("List").Cells.Find(what:=wsht.Cells(intRow, 3)) Is Nothing Then wsht.Cells(intRow, 2) = Worksheets("List").Cells.Find(what:=wsht.Cells(intRow, 3)).Offset(0, -1) End If Next intRow End If Next wsht End Sub