optimizing match formula looping in vba - vba

Lastrow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
For i = 3 To Lastrow
Sheets("sample").Range("AM1000000").End(xlUp).Offset(1, 0).Select
Selection.FormulaArray = _
"=IF(ISNUMBER(MATCH(1," & Chr(10) & " (order!R2C15:R1000000C15=RC[-24])*" & Chr(10) & " (order!R2C7:R1000000C7=RC[-32])*" & Chr(10) & " (order!R2C24:R1000000C24=RC[-15])," & Chr(10) & " 0)), ""pass"",""review"")"
Next i
Columns("AM:AM").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I am using match formula inside of for loop, but it is taking too much time. Is there any way to optimize this code to make it go faster?
Thank you!

There are quite a few improvements you could make, a couple of main ones are:
1) Reading and writing to cells on a worksheet is very slow, as are string manipulations. Instead work out the result first then write it to the sheet rather than write a formula to the cell. Better yet, store the results in an array and write them all out at the end (but that is beyond the scope of this question, you can search about arrays online). You can use application.worksheetfunction to recreate your existing formulas in VBA, or you might consider creating your own function to do it.
2) Avoid selecting cells, again - a very slow operation. Instead use your for loop with the Cells reference to specify the affected cell:
For i = 3 To Lastrow
Sheets("sample").cells(i, 39).FormulaArray = _
"=IF(ISNUMBER(MATCH(1," & Chr(10) & " (order!R2C15:R1000000C15=RC[-24])*" & Chr(10) & " (order!R2C7:R1000000C7=RC[-32])*" & Chr(10) & " (order!R2C24:R1000000C24=RC[-15])," & Chr(10) & " 0)), ""pass"",""review"")"
Next i
The Cells syntax is (row, column). Here i is the row number, 39 is column AM.

By lessening the reference range in the array formulas we can speed it up.
Also removing the clipboard will also speed it up:
lastrow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
For i = 3 To lastrow
With WorksSheets("sample").Range("AM" & i)
.FormulaArray = _
"=IF(ISNUMBER(MATCH(1," & Chr(10) & " (order!R2C15:R" & lastrow & "C15=RC[-24])*" & Chr(10) & " (order!R2C7:R" & lastrow & "C7=RC[-32])*" & Chr(10) & " (order!R2C24:R" & lastrow & "C24=RC[-15])," & Chr(10) & " 0)), ""pass"",""review"")"
.Value = .Value
End With

Related

In VBA trying to create a dynamic Sumifs Formula with multiple criterias in multiple sheets

In VBA I am trying to create a sumifs formula with multiple criteria across different workbooks, but I am struggling on the syntax.
WorkbookRecut.Worksheets("Summary").Activate
Dim CountRows As Long
Dim CountRows2 As Long
CountRows = WorkbookRecut.Worksheets("Summary").Range("I" & WorkbookRecut.Worksheets("Summary").Rows.Count - 1).End(xlUp).Row
CountRows2 = CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Range("I" & CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Rows.Count - 1).End(xlUp).Row
CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Activate
Range("O6").Formula = _
"=Sumifs(" & [WorkbookRecut].Sheets("Summary").Range("I9").Address & ":" & [WorkbookRecut].Sheets("Summary").Range("I" & CountRows).Address _
& "," & [WorkbookRecut].Sheets("Summary").Range("A9").Address & ":" & [WorkbookRecut].Sheets("Summary").Range("A" & CountRows).Address _
& "," & [CashBreaksMetricsWorkbookFinal].Worksheets("CSCIG_Cash Breaks Metrics").Range("K6").Address(Rowabsolute:=False) _
& "," & [WorkbookRecut].Sheets("Summary").Range("D9").Address & ":" & [WorkbookRecut].Sheets("Summary").Range("D" & CountRows).Address _
& "," & [CashBreaksMetricsWorkbookFinal].Worksheets("CSCIG_Cash Breaks Metrics").Range("N6").Address(Rowabsolute:=False) & ")"
CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Range("O6:O" & CountRows2).FillDown
Update
I have updated the most recent code. The only pending issue is the workbooks aren't changing, but all else works as I want :)
When creating a formula string to add to a cell you need to take into account where the different ranges are relative to the sheet where you're going to place the formula. Just calling Address() on one of the inputs may not give you what you want.
You can try something like the code below to abstract that part into a separate function:
Sub Tester()
Dim wsSumm As Worksheet, wsCBM As Worksheet
Dim lr As Long, f
Set wsSumm = WorkbookRecut.Worksheets("Summary")
Set wsCBM = CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics")
lr = wsSumm.Cells(Rows.Count, "I").End(xlUp).Row
f = "=SUMIFS(" & RealAddress(wsCBM, wsSumm.Range("I9:I" & lr)) & "," & _
RealAddress(wsCBM, wsSumm.Range("A9:A" & lr)) & ",$K6," & _
RealAddress(wsCBM, wsSumm.Range("D9:D" & lr)) & ",$N6)"
With wsCBM.Range("O9")
.Formula = f
End With
End Sub
'get a range address for `rngRef`,
' suitable for use in a formula on worksheet `ws`
Function RealAddress(ws, rngRef As Range) As String
Dim s As String
If ws.Parent Is rngRef.Worksheet.Parent Then 'same workbooks?
If Not ws Is rngRef.Worksheet Then s = "'" & rngRef.Worksheet.Name & "'!" 'diff. worksheets?
s = s & rngRef.Address(True, True)
Else
s = rngRef.Address(True, True, external:=True) 'different workbooks
End If
RealAddress = s
End Function
For the formula: You're probably looking for the .Address property from each of your Ranges. Something like Range1.Address & ":" & Range2.Address To get an output like $I$9:$I$307.
But for your Ranges, you need to put the CountRows inside the Range input like WorkbookRecut.Sheets("Summary").Range("A" & CountRows) and then add the .Address to it.
I also agree with #TimWilliams that your formula code could benefit greatly in terms of readability by adding some nicknames for your worksheets.
Here is what your code would look like with those 3 things corrected:
Public CashBreaksMetricsWorkbookFinal As Workbook
Public WorkbookRecut As Workbook
Dim SumSh As Worksheet
Set SumSh = WorkbookRecut.Sheets("Summary")
Dim CountRows As Long
CountRows = SumSh.Range("I" & SumSh.Rows.Count - 1).End(xlUp).Row
Dim CSCIG As Worksheet
Set CSCIG = CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics")
CSCIG.Activate
Range("O9").Formula = _
"=Sumifs(" & SumSh.Range("I9") & ":" & SumSh.Range("I" & CountRows).Address _
& "," & SumSh.Range("A9").Address & ":" & SumSh.Range("A" & CountRows).Address _
& "," & CSCIG.Range("K6").Address _
& "," & SumSh.Range("D9").Address & ":" & SumSh.Range("D" & CountRows).Address _
& "," & CSCIG.Range("N6").Address & ")"
CSCIG.Range("O9").FillDown
We were missing .Address(External:=True)
Thanks all for helping me get there (Finally!)
Final Code Below
Public CashBreaksMetricsWorkbookFinal As Workbook
Public WorkbookRecut As Workbook
Dim CountRows As Long
Dim CountRows2 As Long
CountRows = WorkbookRecut.Worksheets("Summary").Range("I" & WorkbookRecut.Worksheets("Summary").Rows.Count - 1).End(xlUp).Row
CountRows2 = CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Range("I" & CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Rows.Count - 1).End(xlUp).Row
CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Activate
Range("O6").Formula = _
"=Sumifs(" & [WorkbookRecut].Sheets("Summary").Range("I9").Address(External:=True) & ":" & [WorkbookRecut].Sheets("Summary").Range("I" & CountRows).Address(External:=True) _
& "," & [WorkbookRecut].Sheets("Summary").Range("A9").Address(External:=True) & ":" & [WorkbookRecut].Sheets("Summary").Range("A" & CountRows).Address(External:=True) _
& "," & [CashBreaksMetricsWorkbookFinal].Worksheets("CSCIG_Cash Breaks Metrics").Range("K6").Address(Rowabsolute:=False) _
& "," & [WorkbookRecut].Sheets("Summary").Range("D9").Address(External:=True) & ":" & [WorkbookRecut].Sheets("Summary").Range("D" & CountRows).Address(External:=True) _
& "," & [CashBreaksMetricsWorkbookFinal].Worksheets("CSCIG_Cash Breaks Metrics").Range("N6").Address(Rowabsolute:=False) & ")"
CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Range("O6:O" & CountRows2).FillDown
In the formula, you have to double-quote existing quotes:
Change
Sheets("Summary")
to:
Sheets(""Summary"")

SUMIFS Formula in VBA with "<"

Please could you tell me what is wrong with my second last SUMIFS formula where I use " < " &$F$1. All that is returned is FALSE in all the cells. The last SUMIFS without the < works fine.
Sub SumGroups()
Worksheets("Database").Activate
Dim lastCode, lastFiltCode As Integer
Dim Formula As String
'Determine Last Row in Column O (Unfiltered Codes)
lastCode = Range("O" & Rows.Count).End(xlUp).Row
'Filter Unique Codes into Column A Sheet2
Range("O1:O" & lastCode).AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=Sheet2.Range("A1"), Unique:=True
'Determine last Row in Column A (Filtered Codes)
Worksheets("Sheet2").Activate
lastFiltCode = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
'Place SUMIF Formulas in Columns Sheet2
Worksheets("Sheet2").Range("B2:B" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2)"
Worksheets("Sheet2").Range("D2:D" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2,Database!$I$2:$I$" & lastCode & "," < " &$F$1)"
Worksheets("Sheet2").Range("F2:F" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2,Database!$I$2:$I$" & lastCode & ",$F$1)"
End Sub
Just to clarify why the other answers work:
SumIfs (as well as various other Excel functions requiring string operators (such as CountIf) expect that logical operators and their following statements be expressed as a string. I.e. should be enclosed in speech marks e.g. "myString".
Since a formula is also a string e.g. ActiveCell.Formula = "=If(A1=3, 1, 0)" the compiler gets confused about which set of quotation marks denotes the string. So for example this will not work: ActiveCell.Formula = "=If(A1=3, "Yes", "No")".
Technically the way to deal with this is to enclose the required quotation mark in quotation marks of it own; """ myValue """.
However, this quickly becomes confusing. Instead, use the Character function to return the char you require. In this case 34; chr(34) & myvalue & (chr34).
Worksheets("Sheet2").Range("D2:D" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2,Database!$I$2:$I$" & lastCode & ","" < "" &$F$1)"
This will do it.
put < in quotes.
First, in order to get <$F$1 inside the formula I use the Chr(34) to add the parenthesis "" inside the formula.
Besides that, you are mixing-up a few things:
Try to avoid using Worksheets("Database").Activate and Worksheets("Sheet2").Activate, and instead use fully qualified worksheets and ranges, like: With Worksheets("Database") and inside lastCode = .Range("O" & .Rows.Count).End(xlUp).Row.
You have Sheet2 and Worksheets("Sheet2"), these two not always mean the same worksheet. You could rename Sheet2 (by code name) to "Something", and that will be your 2nd sheet, and rename Sheet3 (by code name) to "Sheet2" and you have a problem. So decide on which method you want to use (I prefer to use Worksheets("Sheet2")).
Dim lastCode, lastFiltCode As Integer means lastCode is actually Variant and lastFiltCode is Integer. Anyway, it's better to use Long for both of these variables when you are trying to get the last row.
Code
Option Explicit
Sub SumGroups()
Dim lastCode As Long, lastFiltCode As Long
'Determine Last Row in Column O (Unfiltered Codes)
With Worksheets("Database")
lastCode = .Range("O" & .Rows.Count).End(xlUp).Row
'Filter Unique Codes into Column A Sheet2
.Range("O1:O" & lastCode).AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=Worksheets("Sheet2").Range("A1"), Unique:=True
End With
With Worksheets("Sheet2")
'Determine last Row in Column A (Filtered Codes)
lastFiltCode = .Range("A" & .Rows.Count).End(xlUp).Row
'Place SUMIF Formulas in Columns Sheet2
.Range("B2:B" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2)"
.Range("D2:D" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2,Database!$I$2:$I$" & lastCode & "," & Chr(34) & "<" & Chr(34) & "&$F$1)"
.Range("F2:F" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2,Database!$I$2:$I$" & lastCode & ",$F$1)"
End With
End Sub

Creating a loop with data from other sheets

I'm trying to create a macro for a Personal Budget worksheet, where there is a cell with tedious work.
Using the "Record Macro" button I traced the coding behind the operation I made in the cell, which is the following:
Sub calculo_otherex()
' calculo_otherex Macro
' Cálculo de otros gastos con tarjeta.
'
ActiveCell.FormulaR1C1 = _
"=Extractos!R[-89]C[-2]-('Home Expenses'!R[-105]C+'Home Expenses'!R[-60]C+'Home Expenses'!R[-16]C+Health!R[-105]C+Health!R[-67]C+Health!R[-29]C+Gifts!R[-99]C+'Daily Living'!R[-87]C+'Daily Living'!R[-29]C+'Daily Living'!R[20]C+'Daily Living'!R[59]C+'Daily Living'!R[102]C+Entertainment!R[-105]C+Entertainment!R[-67]C+Entertainment!R[-29]C+Entertainment!R[9]C+Transportation!R[-105]C)"
Range("H129").Select
End Sub
As you can see, in the formula I refer to other sheets, and to specific cells which reflect a result of a certain month (in this case July) and a specific subset (charges to a credit card).
What I want to do is insert a loop so this procedure repeats for all the months, but the problem is that the data in the sheet "Extractos" appears every 7th cell to the right from "Extractos!R[-89]C[-2]", as the other data in the formula appears in the next column.
How could I solve this??
Thank you very much.
Give the following a try... It's hard to read R1C1, so I changed it up a bit and you'll need to replace all my "A1" references to the first month's cell reference. Also, the for loop will only loop 12 times - 12 months in a year. You can manually change this if you'd like. Lastly, the formula will be written in the activecell then the following formulas will be written in the next cell/column to the right.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'instead of using R1C1, use ranges with offsets - it's much easier to read :) ''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'I used "A1" as a placeholder. Replace this with the actual cell reference for the first month
'The first loop will use the "A1" range. Then, the 2nd and following loops will move over 1 or 7 columns
'i = move over 1 column
'x = move over 7 columns
For i = 0 To 11 'this will loop through 12 times 0-11
ActiveCell.Offset(0, i).Formula = _
"=Extractos!" & Range("A1").Offset(0, x).Address _
& "-('Home Expenses'!" & Range("A1").Offset(0, i).Address _
& "+'Home Expenses'!" & Range("A1").Offset(0, i).Address _
& "+'Home Expenses'!" & Range("A1").Offset(0, i).Address _
& "+Health!" & Range("A1").Offset(0, i).Address _
& "+Health!" & Range("A1").Offset(0, i).Address _
& "+Health!" & Range("A1").Offset(0, i).Address _
& "+Gifts!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+Entertainment!" & Range("A1").Offset(0, i).Address _
& "+Entertainment!" & Range("A1").Offset(0, i).Address _
& "+Entertainment!" & Range("A1").Offset(0, i).Address _
& "+Entertainment!" & Range("A1").Offset(0, i).Address _
& "+Transportation!" & Range("A1").Offset(0, i).Address & ")"
'add 7 to x variable
x = x + 7
'loop and add 1 to i
Next i
'macro done, select H129
Range("H129").Select

ranking data using vba

i come to ask your help i need to rank data using vba i have a block of results in column D and i want to rank them in column E without skiping any value so i tried this vba code but it gives me only zeros in all the column then my computer becom slow untill i close the excel file this is the vba code i am using if anyone can help me :
Sub Mactro5()
LastRow = Range("D" & Cells.Rows.Count).End(xlUp).Row
Range("E2:E" & LastRow).Formula = _
"=IF(D2=" & Chr(34) & Chr(34) & "," & Chr(34) & Chr(34) & ",SUMPRODUCT((D$2:D$" & LastRow & ">D2)/COUNTIF(D$2:D$" & LastRow & ",D$2:D$" & LastRow & "&" & Chr(34) & Chr(34) & "))+1)"
End Sub

Insert VBA into formula formatting?

Really quick question on how to format VBA in excel formulas. When you are inserting a formula into excel and you want to insert a variable from vba for example if b is a string you would use " & b & " is that the correct formatting? To illustrate the problem I have the code below and tried to use that formatting and well... I don't know why it wont work, I get a (Compile error: Expected: End of statement). Can anyone tell me where I am going wrong?
Dim HrsSTD As String
Dim HrsSAT As String
Dim HrsSUN As String
Dim HrsSTWN As String
Dim sdFormula
HrsSTD = ActiveCell.Address
Selection.Offset(0, 1).Select
HrsSAT = ActiveCell.Address
Selection.Offset(0, 1).Select
HrsSUN = ActiveCell.Address
Selection.Offset(0, 1).Select
HrsSTWN = ActiveCell.Address
sdFormula = "=IF((" & Range(NamedRange).Cells(2, 1).Address & _
"=""Please add a title"",0,VLOOKUP((" & Range(NamedRange).Cells(2, 1).Address & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)* _
" & HrsSTD & "+VLOOKUP(" & Range(NamedRange).Cells(2, 1).Address & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)* _
" & HrsSAT & "*1.25+VLOOKUP((" & Range(NamedRange).Cells(2, 1).Address & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)*" & HrsSUN & "* _
1.5+VLOOKUP((" & Range(NamedRange).Cells(2, 1).Address & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)*" & HrsSTWN & "*0.75)"
The code I would type into excel would be: But I want to change the A13's and the I16 (i.e. all the relative references) into variables in VBA
=IF(A13="Please add a title",0,VLOOKUP(A13,'Tables (H)'!$H$2:$J$6,2,FALSE)*F16+VLOOKUP(A13,'Tables (H)'!$H$2:$J$6,2,FALSE)*G16*1.25+VLOOKUP(A13,'Tables (H)'!$H$2:$J$6,2,FALSE)*H16*1.5+VLOOKUP(A13,'Tables (H)'!$H$2:$J$6,2,FALSE)*I16*0.75)
Is this what you are trying? Also I see that you haven't taken my advice from the previous answer.
One more tip. Break you code in simple parts. It is easier to understand.
The problem with your code is in the line
",'Tables (H)'!$H$2:$J$6,2,FALSE)* _
" & HrsSAT & "*1.25+VLOOKUP((" & Range(NamedRange).Cells(2, 1).Address & _
You can't write it like that. The first line doesn't have the ending ". You cannot carry it forward to the next line like that.
is this what you are trying?
Dim sFormula As String
Dim sAddr As String
sAddr = Range(NamedRange).Cells(2, 1).Address
sFormula = "=IF(" & sAddr & _
"=""Please add a title"",0,VLOOKUP(" & _
sAddr & ",'Tables (H)'!$H$2:$J$6,2,FALSE)*F16+VLOOKUP(" & _
sAddr & ",'Tables (H)'!$H$2:$J$6,2,FALSE)*G16*1.25+VLOOKUP(" & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)*H16*1.5+VLOOKUP(" & _
sAddr & ",'Tables (H)'!$H$2:$J$6,2,FALSE)*I16*0.75)"