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

后端 未结 8 1442
小蘑菇
小蘑菇 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:06

    I'm pretty new to vba & excel - only been doing this for about 3 months - I thought I'd share my array de-duplication method here as this post seems relevant to it:

    This code if part of a bigger application that analyses pipe data- Pipes are listed in a sheet with number in xxxx.1, xxxx.2, yyyy.1, yyyy.2 .... format. so thats why all the string manipulation exists. basically it only collects the pipe number once only, and not the .2 or .1 part.

            With wbPreviousSummary.Sheets(1)
    '   here, we will write the edited pipe numbers to a collection - then pass the collection to an array
            Dim PipeDict As New Dictionary
    
            Dim TempArray As Variant
    
            TempArray = .Range(.Cells(3, 2), .Cells(3, 2).End(xlDown)).Value
    
            For ele = LBound(TempArray, 1) To UBound(TempArray, 1)
    
                If Not PipeDict.Exists(Left(TempArray(ele, 1), Len(TempArray(ele, 1) - 2))) Then
    
                    PipeDict.Add Key:=Left(TempArray(ele, 1), Len(TempArray(ele, 1) - 2)), _
                                                            Item:=Left(TempArray(ele, 1), Len(TempArray(ele, 1) - 2))
    
                End If
    
            Next ele
    
            TempArray = PipeDict.Items
    
            For ele = LBound(TempArray) To UBound(TempArray)
                MsgBox TempArray(ele)
            Next ele
    
        End With
        wbPreviousSummary.Close SaveChanges:=False
    
        Set wbPreviousSummary = Nothing 'done early so we dont have the information loaded in memory
    

    Using a heap of message boxes for debugging atm - im sure you'll change it to suit your own work.

    I hope people find this useful, Regards Joe

提交回复
热议问题