+ Reply to Thread
Results 1 to 8 of 8

Instr runtime error

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Instr runtime error

    I have this string which contains a hyphen "-". If i run the following code such that my temp_string has a "-" in it ........code will run fine. However, if i remove the "-" from the temp_string then the code has a run time error of "Invalid procedure call or argument".

    I know that the reason i get this run time error is that i am looking for a hyphen in a string that has no hyphen ...........not sure what to do. Need some way of making program not "stop" ......not sure.


    'temp_string = "Denosumab - Bone Loss Oncology"
    temp_string = "Denosumab"
    
    junk1 = InStr(1, temp_string, "-", 1) - 1
    
    junk2 = Left(temp_string, junk1)
    
    junk3 = Trim(junk2)
    
    MsgBox (junk1)
    MsgBox (junk2)
    MsgBox (junk3)
    Last edited by welchs101; 06-08-2011 at 02:35 PM.

  2. #2
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Instr runtime error

    ook, i must be getting tired. I just realized that i shoudl just check to see if the "-" is in the string before i try and create a new string.

  3. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Instr runtime error

    welchs101,

    What the code is trying to do is get all text to the left of the hyphen, then remove leading/ending spaces. If there is no dash, there's no text, so when it tries to get text to the left of the dash you get an error. Try this instead:
    Sub testpart()
        
        Dim temp_string As String, clean_string As String
        
        'temp_string = "Denosumab - Bone Loss Oncology"
        temp_string = "Denosumab"
        
        If InStr(temp_string, "-") > 0 Then
            clean_string = Trim(Left(temp_string, InStr(temp_string, "-") - 1))
            MsgBox clean_string
        Else
            MsgBox "No hyphen found."
        End If
        
    End Sub

    Hope that helps,
    ~tigeravatar

    EDIT: Looks like you got it before I replied

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Instr runtime error

    Hi thanks for the info. I was kinda headed that way.

    I did have another question. See enclosed file.

    I am trying to parse all the values in col-A of sheet1. For some reason when you run this code (again see enclosed macro) when it reaches a string with a "-" in it ......code just seems to end like it finished.


    Dim myinputwrksht As Worksheet
    Dim new_prod_name As String
    Dim junk1 As String
    Set myinputwrksht = Workbooks("Parsing_product_name.xlsm").Worksheets("Sheet1")
    last_row3 = 288
    
    done = False
    x = 2
    Do While Not done
        MsgBox (x)
        If InStr(1, myinputwrksht.Range("A" & x).Value, "-", 1) > 0 Then
            MsgBox ("in this compare thing")
            myinputwrksht.Range("A" & x).Value = Trim(Left(myinputwrksht.Range("A" & x).Value, InStr(1, myinputwrksht.Range("A" & x).Value, "-", 1) - 1))
        End If
        x = x + 1
        If x > last_row3 Then
        done = True
        MsgBox ("done is true")
        End If
    Loop
    Attached Files Attached Files

  5. #5
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Instr runtime error

    welchs101,

    Give this a try instead:
    Sub CleanTextMacro_for_welchs101()
        
        Dim ws As Worksheet: Set ws = ActiveWorkbook.ActiveSheet
        Dim RowIndex As Long, ProductName As String
        For RowIndex = 2 To ws.Range("A" & Rows.Count).End(xlUp).Row
            If InStr(ws.Range("A" & RowIndex).Value, "-") > 0 Then
                ProductName = ws.Range("A" & RowIndex).Value
                ws.Range("A" & RowIndex).Value = Trim(Left(ProductName, InStr(1, ProductName, "-", 1) - 1))
            End If
        Next
        
    End Sub


    Hope that helps,
    ~tigeravatar

  6. #6
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Instr runtime error

    I closed excel and then restarted it.............re-ran the macro AND IT RAN!!!!!!!!!!!!!

    Any ideas why closing down excel and restarting would make it run?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1