JJJ
JJJ

Reputation: 93

InStr function in VBA Excel is returning 0 Even If Specified Spring Occurs in A String

I'm working on string and getting some of it's characters that I need to copy in Excel. It works perfectly fine but when my Windows PC need to be restarted for an update and upon opening it again, the code didn't work anymore. I don't know if this has something to do with the macros being disabled or I need to configure something. Really need help for this one because it's so tiring to manually paste the details cell by cell.

Sub appendDetails()
    Dim jobs As String
    Dim CommandLine As String

    'Helper Variables
    Dim x As Integer
    Dim y As Integer
    Dim z As Integer

    'Gets all the job description
    jobs = Cells(2, "B").Value

    If (jobs <> "") Then
     'Get CommandLine My issue is InStr(jobs, "Command Line : ") is returning 0 when in fact "Command Line : " keyword appears inside job string
     If (InStr(jobs, "Command Line : ") = 0) Then
         ActiveCell.Value = ""
         ActiveCell.Offset(0, 1).Activate

     Else
         x = getPosition(jobs, "Command Line : ") + thisPosition("Command Line : ")
         y = getPosition(jobs, "Host/Host Group : ")
         z = getLengthOfString(x, y)
         CommandLine = getString(jobs, x, z)
         ActiveCell.Value = Trim(Replace(CommandLine, Chr(10), ""))
         ActiveCell.Offset(0, 1).Activate
         MsgBox ("Command Line : " + CommandLine)
     End If
  End Sub

For clarifications, this sub function calls other functions that I haven't included here. Just if you are curious about the functions.

Upvotes: 0

Views: 1314

Answers (1)

jsotola
jsotola

Reputation: 2278

this is your answer that i posted in the comments earlier

InStr is returning 0 because the specified substring is not occurring in the string. use debug.print to check your values

Upvotes: 1

Related Questions