Duranchula
Duranchula

Reputation: 47

VBA InStr function

HI I am trying to search for a sub string within a string and if the sub string is found then I want to paste the output to my spreadsheet. I am using th InStr function with a conditional statement <> , so that if mt InStr function value is not equal to 0 I know I have a match. I do not get any errors when I run the code but it does not display the result on the sheet. SOS!! :)

Sub unicornhorn()
'Generates user friendly graphs from raw unicorn input data

    Dim columns As Integer
    Dim rows As Integer
    Dim Outputs(50) As String
    Dim LastRow As Integer
    Dim StrG As Integer
    Dim xaxis As Range
    Dim yaxis As Range

    Dim nLeft As Double: nLeft = 20
    Dim nTop As Double: nTop = 20
    Dim start As Double: start = start + 2
    Dim finish As Double: finish = finish + 2


    Dim Curves(14) As String
        Curves(0) = "260nm"
        Curves(1) = "280nm"
        Curves(2) = "214nm"
        Curves(3) = "Cond"
        Curves(4) = "Cond%"
        Curves(5) = "Conc"
        Curves(6) = "pH"
        Curves(7) = "Pressure"
        Curves(8) = "Flow"
        Curves(9) = "Temp"
        Curves(10) = "Fractions"
        Curves(11) = "inject"
        Curves(12) = "logbook"
        Curves(13) = "P960_Press"
        Curves(14) = "P960_Flow"

    Dim D As Worksheet
    Set D = Worksheets("DATA")
    Dim C As Worksheet
    Set C = Worksheets("sheet1")


    'defines data range
    columns = shCurves.Cells(1, shCurves.columns.Count).End(xlToLeft).Column
    'XXreturn check to sheet
    ShData.Cells(5, 5).Value = columns
    rows = shCurves.Cells(shCurves.rows.Count, 1).End(xlUp).Row
    'XXreturn check to sheet
    ShData.Cells(5, 6).Value = rows


    For Z = 0 To columns

        'loops through array for different curves
        Outputs(Z) = shCurves.Cells(2, Z * 2 + 1).Value

        'XXreturn check to sheet
        ShData.Cells(5 + Z, 7).Value = Outputs(Z)

        'Finds last row in column for current curve
        LastRow = Cells(D.rows.Count, Z * 2 + 1).End(xlUp).Row


        For k = 0 To 14

            'finds curve identifyer within string
            StrG = InStr("Outputs(Z)", "Curves(k)")
            If StrG <> 0 Then


            ShData.Cells(25 + k, 5).Value = Curves(k)


            Exit For
            End If                  


        Next k

    Next Z

End Sub

Upvotes: 1

Views: 1160

Answers (1)

Gratzy
Gratzy

Reputation: 9389

Why do you have double quotes around your arrays?

StrG = InStr("Outputs(Z)", "Curves(k)")

You're using the string literals, not the variables. Remove the double quotes:

StrG = InStr(Outputs(Z), Curves(k))

Upvotes: 4

Related Questions