Make formula to macro

Alvaroro84

Board Regular
Joined
May 13, 2022
Messages
65
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I tried to make a macro out of this formula while it works on one cell i cant get it to work with the rest of the column
Excel Formula:
=SUM(SUMIFS(C5:O5,$C$4:$O$4,{"Last","chev","tev","Pys"}))

This formula above works exactly like its suppose to. When i drop it down to the rest of the column it keep this "$C$4:$O$4" constant and changes only this "C5:O5" which is exactly how i want it to work and i tried to build a macro using that same logic. Issue with the bottom macro is that it only stays as =SUM(SUMIFS(C5:O5,$C$4:$O$4,{"Last","chev","tev","Pys"})) and through out the whole column it doesn't change this part "C5:O5"

VBA Code:
Sub SumIfsMultipleOR()
    Dim sumRange As Range
    Dim criteriaRange As Range
    Dim result As Double
    Dim i As Integer
  
    Set sumRange = Range("C5:O5")
    Set criteriaRange = Range("$C$4:$O$4")
  
    Dim criteria As Variant
    criteria = Array("Last", "chev", "tev", "Pys")
    For i = 0 To UBound(criteria)
        result = WorksheetFunction.Sum(result, _
                    WorksheetFunction.SumIfs(sumRange, criteriaRange, criteria(i)))
    Next i
  
    Range("V:V") = result
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I can't test at the moment, but all you should need to do is iterate the sumRange inside the loop. give this a go.
VBA Code:
Sub SumIfsMultipleOR()
    Dim sumRange As Range
    Dim criteriaRange As Range
    Dim result As Double
    Dim i As Integer

    Set criteriaRange = Range("$C$4:$O$4")
  
    Dim criteria As Variant
    criteria = Array("Last", "chev", "tev", "Pys")
    For i = 0 To UBound(criteria)
        Set sumRange = Range("C"&i+5&":O"&i+5")
        result = WorksheetFunction.Sum(result, _
                    WorksheetFunction.SumIfs(sumRange, criteriaRange, criteria(i)))
    Next i
  
    Range("V:V") = result
End Sub
 
Upvote 0
I can't test at the moment, but all you should need to do is iterate the sumRange inside the loop. give this a go.
VBA Code:
Sub SumIfsMultipleOR()
    Dim sumRange As Range
    Dim criteriaRange As Range
    Dim result As Double
    Dim i As Integer

    Set criteriaRange = Range("$C$4:$O$4")
 
    Dim criteria As Variant
    criteria = Array("Last", "chev", "tev", "Pys")
    For i = 0 To UBound(criteria)
        Set sumRange = Range("C"&i+5&":O"&i+5")
        result = WorksheetFunction.Sum(result, _
                    WorksheetFunction.SumIfs(sumRange, criteriaRange, criteria(i)))
    Next i
 
    Range("V:V") = result
End Sub
Im getting an issue where i marked the lettering yellow Set sumRange = Range("C"&i+5&":O"&i+5") the message says "expected: list separator or )"
 
Upvote 0
seems i added an extra speech mark.
VBA Code:
Sub SumIfsMultipleOR()
    Dim sumRange As Range
    Dim criteriaRange As Range
    Dim result As Double
    Dim i As Integer

    Set criteriaRange = Range("$C$4:$O$4")
  
    Dim criteria As Variant
    criteria = Array("Last", "chev", "tev", "Pys")
    For i = 0 To UBound(criteria)
        Set sumRange = Range("C"&i+5&":O"&i+5)
        result = WorksheetFunction.Sum(result, _
                    WorksheetFunction.SumIfs(sumRange, criteriaRange, criteria(i)))
    Next i
  
    Range("V:V") = result
End Sub
 
Upvote 0
seems i added an extra speech mark.
VBA Code:
Sub SumIfsMultipleOR()
    Dim sumRange As Range
    Dim criteriaRange As Range
    Dim result As Double
    Dim i As Integer

    Set criteriaRange = Range("$C$4:$O$4")
 
    Dim criteria As Variant
    criteria = Array("Last", "chev", "tev", "Pys")
    For i = 0 To UBound(criteria)
        Set sumRange = Range("C"&i+5&":O"&i+5)
        result = WorksheetFunction.Sum(result, _
                    WorksheetFunction.SumIfs(sumRange, criteriaRange, criteria(i)))
    Next i
 
    Range("V:V") = result
End Sub
Its odd I keep getting the message on the same spot
 
Upvote 0
Same i just pasted it back in, apparently the gui doesn't like no spaces.

VBA Code:
Set sumRange = Range("C" & i + 5 & ":O" & i + 5)
appears to be fine though
 
Upvote 0
Same i just pasted it back in, apparently the gui doesn't like no spaces.

VBA Code:
Set sumRange = Range("C" & i + 5 & ":O" & i + 5)
appears to be fine though
It works unfortunately it still only populates one number on all the cells
 
Upvote 0
weird i focused on the type and not the rest of the code. it will, the result gets populated in the loop and overwrites itself every time so only the final iteration of the loop will count.

Are you expecting 4 results in the v column? you'll have to move the write into the loop.
This would write starting in cell V1

VBA Code:
Sub SumIfsMultipleOR()
    Dim sumRange As Range
    Dim criteriaRange As Range
    Dim result As Double
    Dim i As Integer

    Set criteriaRange = Range("$C$4:$O$4")
  
    Dim criteria As Variant
    criteria = Array("Last", "chev", "tev", "Pys")
    For i = 0 To UBound(criteria)
        Set sumRange = Range("C" & i + 5 & ":O" & i + 5)
        result = WorksheetFunction.Sum(result, _
                    WorksheetFunction.SumIfs(sumRange, criteriaRange, criteria(i)))
        Range("V" & i + 1) = result
    Next i
  
End Sub
 
Upvote 0
weird i focused on the type and not the rest of the code. it will, the result gets populated in the loop and overwrites itself every time so only the final iteration of the loop will count.

Are you expecting 4 results in the v column? you'll have to move the write into the loop.
This would write starting in cell V1

VBA Code:
Sub SumIfsMultipleOR()
    Dim sumRange As Range
    Dim criteriaRange As Range
    Dim result As Double
    Dim i As Integer

    Set criteriaRange = Range("$C$4:$O$4")
 
    Dim criteria As Variant
    criteria = Array("Last", "chev", "tev", "Pys")
    For i = 0 To UBound(criteria)
        Set sumRange = Range("C" & i + 5 & ":O" & i + 5)
        result = WorksheetFunction.Sum(result, _
                    WorksheetFunction.SumIfs(sumRange, criteriaRange, criteria(i)))
        Range("V" & i + 1) = result
    Next i
 
End Sub
This specific one has about 1330 result but usually varies is there a way to stop it on the condition that column A is empty
 
Upvote 0

Forum statistics

Threads
1,215,712
Messages
6,126,408
Members
449,313
Latest member
tomgrandy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top