IF / ELSE语句中的Qualifier VBA无效

我在使用VBA(几个星期前刚刚开始学习)方面相当新颖,我试图创build一个macros,在表中search特定的短语,然后将相应行中的数字复制到另一个表中, PO。

用于收集信息的表单不断变化,因为它是为特定作业创build的,因此必须执行search才能find需要PO的正确作业。 一旦发现它将“旅行时间”中的总量信息和数量一直到“运费”,然后将它们放入PO模板的相应部分。

当我testing它时,我得到一个错误,说“无效的限定符”我已经在代码中评论。

除了那个错误之外,我相信要做到这一点还有一个更简单的方法。 我全部都是关于学习新东西,所以请随时编辑需要的地方。

提前感谢您的帮助!

这里是代码清单:

Sub Extract_job_info() Dim Title As String Dim Param As String Dim Message As String Dim defaultRef As String Dim Sht As Worksheet Dim WorksheetExists As Boolean Dim CreatePO As Integer Dim InRowB As Long Dim InColB As Range Set POSheet = Sheets("Request for PO Template") 'create an input box to ask for job number to exract to a PO 'set message details Title = "Job Number" Message = "Please enter the job number you would like to extract information from." defaultRef = "Enter job number here" 'input box Param = InputBox(Message, Title, defaultRef) 'for loop to check if job exists For Each Sht In ThisWorkbook.Worksheets 'Following line ignores case in comparison If UCase(Sht.Name) = UCase(Param) Then WorksheetExists = True Exit For End If Next Sht 'If job does not exist If WorksheetExists = False Then MsgBox ("Job number does not exist") 'if job exists Else CreatePO = MsgBox("Would you like to extract job number " & Param & " to make a PO?", vbYesNo, Confirm) 'if user does not want to create a PO If CreatePO = vbNo Then Exit Sub 'if user wants to create a PO 'find total travel hours ElseIf CreatePO = vbYes Then InRowB = 1 'for testing Set InColB = Sht.Rows(InRowB).Find(What:="Cost", _ LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _ SearchDirection:=xlNext, MatchCase:=False) If Not InColB Is Nothing Then POSheet.Range("F30").Value = InColB.End(xlToRight).Value Else MsgBox "'Cost' cell not found!", vbCritical Exit Sub End If 'copy total travel hours 'object variable or with block variable not set InColB.End(xlToRight).Copy 'paste total travel hours into PO POSheet.Range("F30").Select Selection.PasteSpecial Paste:=xlPasteValues End If End If End Sub 

编辑:编译但未经testing

 Sub Extract_job_info() Dim Title As String Dim Param As String Dim Message As String Dim defaultRef As String Dim Sht As Worksheet, shtJob As Worksheet Dim POSheet As Worksheet Dim CreatePO As Integer Dim InRowB As Long Dim InColB As Range Set POSheet = Sheets("Request for PO Template") 'set message details Title = "Job Number" Message = "Please enter the job number you would like to extract information from." defaultRef = "Enter job number here" Param = InputBox(Message, Title, defaultRef) 'find sheet For Each Sht In ThisWorkbook.Worksheets If UCase(Sht.Name) = UCase(Param) Then Set shtJob = Sht Exit For End If Next Sht 'If job does not exist If shtJob Is Nothing Then MsgBox ("Sheet for '" & Param & "' was not found !") Else If MsgBox("Would you like to extract job number '" & _ Param & "' to make a PO?", vbYesNo, "Confirm") = vbYes Then InRowB = 1 'for testing Set InColB = Sht.Rows(InRowB).Find(What:="Cost", _ LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _ SearchDirection:=xlNext, MatchCase:=False) If Not InColB Is Nothing Then POSheet.Range("F30").Value = InColB.End(xlToRight).Value Else MsgBox "'Cost' cell not found!", vbCritical End If 'found "cost" End If 'user confirmed extract End If 'got sheet End Sub 
 Dim InColB As String ... InColB.End(xlToRight).Copy 

InColB是一个string,而不是一个范围。 该操作不会在string上工作