Deleting Elements in an Array if Element is a Certain value VBA

后端 未结 8 1384
小蘑菇
小蘑菇 2020-11-27 20:14

I have a global array, prLst() that can of variable length. It takes in numbers as strings \"1\" to Ubound(prLst). However, when the

相关标签:
8条回答
  • 2020-11-27 21:09

    Deleting Elements in an Array if Element is a Certain value VBA

    to delete elements in an Array wih certain condition, you can code like this

    For i = LBound(ArrValue, 2) To UBound(ArrValue, 2)
        If [Certain condition] Then
            ArrValue(1, i) = "-----------------------"
        End If
    Next i
    
    StrTransfer = Replace(Replace(Replace(join(Application.Index(ArrValue(), 1, 0), ","), ",-----------------------,", ",", , , vbBinaryCompare), "-----------------------,", "", , , vbBinaryCompare), ",-----------------------", "", , , vbBinaryCompare)
    ResultArray = join( Strtransfer, ",")
    

    I often manipulate 1D-Array with Join/Split but if you have to delete certain value in Multi Dimension I suggest you to change those Array into 1D-Array like this

    strTransfer = Replace(Replace(Replace(Replace(Names.Add("A", MultiDimensionArray), Chr(34), ""), "={", ""), "}", ""), ";", ",")
    'somecode to edit Array like 1st code on top of this comment
    'then loop through this strTransfer to get right value in right dimension
    'with split function.
    
    0 讨论(0)
  • 2020-11-27 21:09

    I know this is old, but here's the solution I came up with when I didn't like the ones I found.

    -Loop through the array (Variant) adding each element and some divider to a string, unless it matches the one you want to remove -Then split the string on the divider

    tmpString=""
    For Each arrElem in GlobalArray
       If CStr(arrElem) = "removeThis" Then
          GoTo SkipElem
       Else
          tmpString =tmpString & ":-:" & CStr(arrElem)
       End If
    SkipElem:
    Next
    GlobalArray = Split(tmpString, ":-:")
    

    Obviously the use of strings creates some limitations, like needing to be sure of the information already in the array, and as-is this code makes the first array element blank, but it does what I need and with a little more work it could be more versatile.

    0 讨论(0)
提交回复
热议问题