简体   繁体   中英

VBA Array/Dictionary IF formula and SUMIF

I'm trying to switch to VBA arrays with one of my Excel Workbook as it has multiple sheets which are referencing a sheet with 50K + rows and columns from A to AK. There's a bunch of IFs,SUMIFs and VLookUps. It takes about 10 minutes for excel to fully calculate all the formulas and complete the data I need, then some other minutes to save what I need with values only and no formulas.

I'm new to VBA and only managed to convert the vlookups and that cut the processing time of said formulas to almost instant. Now I'm trying to convert the IF/SUMifs formulas...but for some reason I just can't wrap my mind about how to approach this/find a solution online.

Using the below code I'm adding the two columns I need for my IF formula, about this I'm about 90% sure as I'm using the same one(reading different columns) for the vlookup code which works perfectly. So it should work here as well for mapping the dictionary and array.

'Array variable
Dim arr() As Variant
'Dictionary object
Dim dic As Object: Set dic = CreateObject("Scripting.Dictionary")


'Read data into array
With Sheets("test")
    x = .Cells(.Rows.Count, 2).End(xlUp).Row
    'Range(C2:Cx where x is last row in column C)
    arr = .Cells(2, 3).Resize(x - 1).Value
End With


'Map arr into dictionary
For x = LBound(arr, 1) To UBound(arr, 1)
    'dic(match column) = return column
    dic(arr(x, 1)) = arr(x, 3)
Next x
'Clear array contents
Erase arr


With Sheets("test")
    x = .Cells(.Rows.Count, 4).End(xlUp).Row
    'Read values from column D into array
    'Range(D2:Dx, where x is last row in column D)
    arr = .Cells(2, 4).Resize(x - 1).Value

Now I'm trying to convert the following formula =IF(ROW(A2)=1,"Vendor",IF(D2="",C2," **"&C2)) into VBA. I guess the ideal way would be through a loop and to store the values in arr and then output to my desired column, but I can't think of an approach for this. I've tried a For combined with IF but with no success. This is what I've tried with no success:

For x = LBound(arr, 1) To UBound(arr, 1)
If dic(arr(x, 1)) <> "" Then
arr(x, 1) = "**" & arr(x,1)

Also I have a bunch of SUMIFs which are basically the same formula with different
criteria : =SUMIFS(L:L,AE:AE,AE2,E:E,E2,AF:AF,AF2) . But for this one except for mapping the whole sheet into an array/dictionary I have no idea what to do.

I'm new to all VBA and at this point I'm entirely stuck. Any help would be useful:).

You don't need a Dictionary here. Better use an 2-D Array and another Array for output. Then just loop and assign the values like the following:

'Read data into array
With Sheets("test")
    x = .Cells(.Rows.Count, 2).End(xlUp).Row
    'Range(C2:Cx where x is last row in column C)
    arr = .Cells(2, 3).Resize(x - 1, 2).Value   ' CHANGE: All needed columns

    ReDim OutArr(1 To UBound(arr))
    For x = 1 To UBound(arr)
        OutArr(x) = IIf(arr(x, 2) = "", " **", "") & arr(x, 1)
    Next x
    .Cells(2, 5).Resize(UBound(OutArr)).Value = OutArr  ' Change column if needed

End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM