这是我的第一篇文章 - 虽然我现在已经潜伏了一段时间。我是VBA的新手,我从这个论坛学到了很多东西,谢谢大家。
我的代码接受一个字符串,打开另一个excel文件并尝试在打开的文件的第一列中搜索字符串。我收到错误:
“vba错误:对象变量或者没有设置块变量”.find我相信。非常感谢您的帮助。我尝试了各种各样的事情,但说实话,我觉得我在黑暗中作战。
我的代码:
Option Explicit
Private Sub GetProductCode()
'Opens jobs file
'Trims down customer reference column to job number only, then searches job file to return product code
Dim sJobNumber As String
Dim sCustomerReference As String
Dim rownum As Long
Dim wbkJobs As Workbook
Dim wsJobs As Worksheet
Dim strPathFile As String
Dim strSearch As String
Dim x As Range
On Error GoTo ErrHandler
Application.ScreenUpdating = False
'Change as desired
strPathFile = "\\ACHILLES\Company\Production_schedule\Jobs.xlsm"
strSearch = "Specific text"
Set wbkJobs = Workbooks.Open _
(Filename:=strPathFile, _
UpdateLinks:=0, _
ReadOnly:=True, _
AddToMRU:=False)
'MsgBox wbkJobs.Name
rownum = 2
'Refer to sheet using code name (3)
sCustomerReference = ThisWorkbook.Sheets(3).Range("E" & rownum).Value
'MsgBox sCustomerReference
sJobNumber = Right(sCustomerReference, 5)
'MsgBox sJobNumber
strSearch = ("*" & sJobNumber)
'MsgBox strSearch
Set wsJobs = ActiveWorkbook.Sheets("Jobs")
Set x = wsJobs.Range("A:A").Find(What:=strSearch, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)