所以我试图在另一个工作簿中找到包含String PartNumber的单元格。我还没有找到办法让这项工作成功。我错过了什么?
Private Sub SearchInventory_Click()
Dim PartNumber as String
Dim PartRange As Range
PartNumber = Application.InputBox("Enter the Part Number")
Application.ScreenUpdating = False
Activewindow.WindowState = xlMinimized
Workbooks.Open ("C:\Users\Dan\Desktop\Assembly Sheet\Inventory.xlsx")
Activewindow.WindowState = xlMaximized
Workbooks("Inventory.xlsx").Activate
Set PartRange = Workbooks("Inventory.xlsx").Worksheets("Inventory").Range("A:A").Find(PartNumber, , xlValues, xlWhole).Select
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
您试图将Select
的值视为一个对象(带有set
),这在VBA中是非法的,因为select
返回一个布尔值(不是对象) )。另一个问题是,如果找不到部件select
将在nothing
上调用,这也会破坏。
您需要分两步执行此操作:
Private Sub SearchInventory_Click()
Dim PartNumber as String
Dim PartRange As Range
PartNumber = Application.InputBox("Enter the Part Number")
Application.ScreenUpdating = False
Activewindow.WindowState = xlMinimized
Workbooks.Open ("C:\Users\Dan\Desktop\Assembly Sheet\Inventory.xlsx")
Activewindow.WindowState = xlMaximized
Workbooks("Inventory.xlsx").Activate
'Find but do not select so we take the value of Find
Set PartRange = Workbooks("Inventory.xlsx").Worksheets("Inventory").Range("A:A").Find(PartNumber, , xlValues, xlWhole)
'select only if it found something, tell the user otherwise
If PartRange Is Nothing Then
MsgBox("Part not found!")
Else
PartRange.Select
End If
Application.ScreenUpdating = True
End Sub