VBA InStr函数

时间:2016-02-24 18:21:28

标签: vba if-statement

HI我正在尝试在字符串中搜索子字符串,如果找到子字符串,那么我想将输出粘贴到我的电子表格中。我正在使用条件语句<>的InStr函数,如果mt InStr函数值不等于0,我知道我有一个匹配。我运行代码时没有出现任何错误,但它不会在工作表上显示结果。 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

1 个答案:

答案 0 :(得分:4)

为什么数组周围有双引号?

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

您使用的是字符串文字,而不是变量。删除双引号:

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