last10seconds
last10seconds

Reputation: 333

Invalid Qualifier VBA in IF/ELSE statement

I am fairly new at using VBA (just started learning it a couple weeks ago), and I am trying to create a macro that searches for a specific phrase within the sheet and then copies the numbers in that corresponding row into another sheet that creates a PO.

The sheet that is used to gather the information is ever changing because it is created for specific jobs so a search has to be done to find the correct job that needs a PO. once that is found it takes the total information and quantities from "travel hours" all the way to "freight" and then places them into the corresponding sections in the PO template.

When I test it I get an error that says "invalid qualifier" I have it commented in the code.

On top of that error, I'm sure there has to be an easier way going about doing this. I'm all about learning new things, so please feel free to edit where it is needed.

Thanks for any help in advance!

here is the code listing:

       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

Upvotes: 0

Views: 625

Answers (2)

Tim Williams
Tim Williams

Reputation: 166126

EDIT: compiled but not tested

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

Upvotes: 0

Raystafarian
Raystafarian

Reputation: 3022

Dim InColB As String
...
InColB.End(xlToRight).Copy

InColB is a string, not a range. That operation won't work on a string

Upvotes: 1

Related Questions