VBA excel循环通过2个阵列

时间:2017-05-17 14:56:57

标签: arrays vba excel-vba excel

我正在填充两个数组,其中一个值包含在内,另一个数组用于排除。一切都在努力。下一部分应该从每个数组中获取值,并用空格替换不需要的值。这也适用但仅适用于第一个值。我知道我需要一个循环,但不能理解它。任何指针都会有所帮助。如果有更好的方法,我全都耳朵。

Select * FROM table WHERE myJSONArr.indexOf("s") = 3 or 4

2 个答案:

答案 0 :(得分:1)

添加此sub并替换不使用sub调用的部分应该有效。

Sub RemoveUnwantedValues(ByRef result As String, del)

    Dim i           As Integer
    Dim arrResult() As String


    arrResult = Split(result, " ")

    For i = LBound(arrResult) To UBound(arrResult)
        arrResult(i) = Trim(Replace(Replace(Replace(arrResult(i), del, ""), del, ""), "  ", " "))
        arrResult(i) = Replace(arrResult(i), " ", ",")
    Next i

End Sub

您可以这样称呼它:RemoveUnwantedValues result, del

答案 1 :(得分:0)

感谢您的帮助。事实证明,我过度复杂化了。 所有我最终需要做的是删除结果删除循环iii上的连接并放置如果instr,del&在循环ii中的ServiceSym。

Sub Service_Symbols()

Application.ScreenUpdating = False
Dim StringArray() As String

Dim i As Long

For i = Sheet2.Cells(Rows.Count, 11).End(xlUp).Row To 2 Step -1
'Seperate multiple values in cells
If InStr(Cells(i, 11).Value, ",") <> 0 Then
    StringArray() = Split(Cells(i, 11).Value, ",")
    'Place selected values into array for inclusion
    For ii = LBound(StringArray) To UBound(StringArray)
        If IsInArray(StringArray(), "1") Or IsInArray(StringArray, "4") Or IsInArray(StringArray, "5") Or IsInArray(StringArray, "6") Or IsInArray(StringArray, "7") Or IsInArray(StringArray, "8") Then
            result = StringArray(ii)
            'Debug.Print i; result
        End If
        If InStr(result, "2") <> 0 Or InStr(result, "3") <> 0 Or InStr(result, "9") <> 0 Or InStr(result, "11") <> 0 Then
            del = result
            'Debug.Print i; "del-"; del
        End If
    ServiceSym = Replace(result, del, "")
    'Sheet1.Range("G" & i).Value = ServiceSym
    Debug.Print i; ServiceSym

    Next ii

'transfer selected single values in cells
ElseIf Sheet2.Range("K" & i).Value = "1" Or Sheet2.Range("K" & i).Value = "4" Or Sheet2.Range("K" & i).Value = "5" Or Sheet2.Range("K" & i).Value = "6" Or Sheet2.Range("K" & i).Value = "7" Or Sheet2.Range("K" & i).Value = "8" Then
result2 = Sheet2.Range("K" & i).Value
'Sheet1.Range("G" & i).Value = result2
Debug.Print i; result2
End If
Next i

Application.ScreenUpdating = True

'Call More_Services_Symbols 'Run the more services sub

End Sub