SumIf / do until loop

hansgrandia

Board Regular
Joined
Jan 10, 2015
Messages
50
[COLOR=rgba(0, 0, 0, 0.8)]Hello,[/COLOR]

[COLOR=rgba(0, 0, 0, 0.8)]Instead of using a pivot to calculate totals for my subcategories, I would like to use the sumIf function in Excel VBA. [/COLOR]
[COLOR=rgba(0, 0, 0, 0.8)]After I dumped my transacation (variable length each month) in Sheet1 (in dutch: "Blad1"), I'm aiming to calculate the totals on a separate sheet ("Som transacties")[/COLOR]

[COLOR=rgba(0, 0, 0, 0.8)]I have defined app 20 subcategories which should be visible in row A2 until row A21 (A1 is a header).[/COLOR]

[COLOR=rgba(0, 0, 0, 0.8)]After running the code, I get a 1004 error which I do not really understand. [/COLOR]

[COLOR=rgba(0, 0, 0, 0.8)]Could someone help me out and double check to below standing code? [/COLOR]

[COLOR=rgba(0, 0, 0, 0.8)]Very much appreciated![/COLOR]
[COLOR=rgba(0, 0, 0, 0.8)]Hans Grandia[/COLOR]
[COLOR=rgba(0, 0, 0, 0.8)]The Netherlands[/COLOR]

.................................................................................................

Sub SumIfTransacties()


Dim Transacties As Range
Dim Sum As Integer


Set Transacties = Worksheets("Blad1").Range("D2").End(xlDown)


'Add sheet with totals per subcategory
Worksheets.Add After:=Sheets("Blad1")
Worksheets.Add.Name = "Som Transacties"


'Subcategories in Column A
Range("A1").Value = "Subcategorie"
Range("A2").Value = "ANWB"
Range("A3").Value = "Autoverzekering"
Range("A4").Value = "Bankkosten"
Range("A5").Value = "Eten & drinken en persoonlijke verzorging"
Range("A6").Value = "Kleding"
Range("A7").Value = "Kranten / weekbladen / kerkblad / boeken"
Range("A8").Value = "Lasten woning"
Range("A9").Value = "Lidmaatschap kerk en goede doelen"
Range("A10").Value = "Onderhoud auto"
Range("A11").Value = "Opleiding en persoonlijke ontwikkeling"
Range("A12").Value = "Overig"
Range("A13").Value = "Reisverzekering"
Range("A14").Value = "Sport"
Range("A15").Value = "Uitvaartverzekering"
Range("A16").Value = "Vakantie en ontspanning"
Range("A17").Value = "Vakbond"
Range("A18").Value = "Vervoer"
Range("A19").Value = "Wegenbelasting"
Range("A20").Value = "Zakgeld / cadeaus / boetes"
Range("A21").Value = "Ziektenkosten"


'Select first blank cell of subcategory (Column B)
Worksheets("Som Transacties").Activate
Range("A2").Select


Do Until ActiveCell.Value = ""


ActiveCell.Offset(0, 1).Select


Sum = Application.WorksheetFunction.SumIf(Range("Transacties"), Range("A2"), (Range("Transacties")))


' Move 1 row down
Worksheets("Som Transacties").Activate
ActiveCell.Offset(1, 0).Select


Loop


Columns("A:A").EntireColumn.AutoFit


End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

williamgowtama

Board Regular
Joined
Apr 12, 2015
Messages
123
put this code

Code:
[COLOR=#333333]Worksheets("Blad1").[/COLOR][COLOR=#333333]Activate

[/COLOR][COLOR=#333333]'Subcategories in Column A[/COLOR]
[COLOR=#333333]Range("A1").Value = "Subcategorie"[/COLOR]
[COLOR=#333333]Range("A2").Value = "ANWB"[/COLOR]
[COLOR=#333333]Range("A3").Value = "Autoverzekering"[/COLOR]
[COLOR=#333333]Range("A4").Value = "Bankkosten"[/COLOR]
[COLOR=#333333]Range("A5").Value = "Eten & drinken en persoonlijke verzorging"[/COLOR]
[COLOR=#333333]Range("A6").Value = "Kleding"[/COLOR]
[COLOR=#333333]Range("A7").Value = "Kranten / weekbladen / kerkblad / boeken"[/COLOR]
[COLOR=#333333]Range("A8").Value = "Lasten woning"[/COLOR]
[COLOR=#333333]Range("A9").Value = "Lidmaatschap kerk en goede doelen"[/COLOR]
[COLOR=#333333]Range("A10").Value = "Onderhoud auto"[/COLOR]
[COLOR=#333333]Range("A11").Value = "Opleiding en persoonlijke ontwikkeling"[/COLOR]
[COLOR=#333333]Range("A12").Value = "Overig"[/COLOR]
[COLOR=#333333]Range("A13").Value = "Reisverzekering"[/COLOR]
[COLOR=#333333]Range("A14").Value = "Sport"[/COLOR]
[COLOR=#333333]Range("A15").Value = "Uitvaartverzekering"[/COLOR]
[COLOR=#333333]Range("A16").Value = "Vakantie en ontspanning"[/COLOR]
[COLOR=#333333]Range("A17").Value = "Vakbond"[/COLOR]
[COLOR=#333333]Range("A18").Value = "Vervoer"[/COLOR]
[COLOR=#333333]Range("A19").Value = "Wegenbelasting"[/COLOR]
[COLOR=#333333]Range("A20").Value = "Zakgeld / cadeaus / boetes"[/COLOR]
[COLOR=#333333]Range("A21").Value = "Ziektenkosten"[/COLOR]

the error caused by the vba cannot find the range or cell that has the stated value.



--------------------------------------------------


Please Give me like if my post helps


Need my help further?


id.linkedin.com/in/williamgowtama


Thank you :D
 

hansgrandia

Board Regular
Joined
Jan 10, 2015
Messages
50
Hi,
thanks you for the quick response! Did you include the full code or do I miss something?
If you could help me out, no problem to like you on Linkedin.
Thanks again,
Hans
 

williamgowtama

Board Regular
Joined
Apr 12, 2015
Messages
123
After further checking your code, i found 2 places that need to be fixed,

Code:
[COLOR=#333333]Sum = Application.WorksheetFunction.SumIf([B]Range("Transacties")[/B], Range("A2"), (Range("Transacties")))[/COLOR]

the range("transacties") should be added a validation like range("transacties") = 3 or range("transacties") <> 0

Worksheets("Blad1").Activate
'Subcategories in Column A
Range("A1").Value = "Subcategorie"
Range("A2").Value = "ANWB"
Range("A3").Value = "Autoverzekering"
Range("A4").Value = "Bankkosten"
Range("A5").Value = "Eten & drinken en persoonlijke verzorging"
Range("A6").Value = "Kleding"
Range("A7").Value = "Kranten / weekbladen / kerkblad / boeken"
Range("A8").Value = "Lasten woning"
Range("A9").Value = "Lidmaatschap kerk en goede doelen"
Range("A10").Value = "Onderhoud auto"
Range("A11").Value = "Opleiding en persoonlijke ontwikkeling"
Range("A12").Value = "Overig"
Range("A13").Value = "Reisverzekering"
Range("A14").Value = "Sport"
Range("A15").Value = "Uitvaartverzekering"
Range("A16").Value = "Vakantie en ontspanning"
Range("A17").Value = "Vakbond"
Range("A18").Value = "Vervoer"
Range("A19").Value = "Wegenbelasting"
Range("A20").Value = "Zakgeld / cadeaus / boetes"
Range("A21").Value = "Ziektenkosten"
Before assigning values, for example range("A1").value = "...", you have to activate the sheet that you want to put value on.
Worksheets("Blad1").Activate

'or

Worksheets("Som Transacties").Activate
 
Last edited:

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,140
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile

ADVERTISEMENT

Transacties is a variable not a named range... (although I'll be surprised if the second Transacties should be there as it should be values).
It is also a single cell so not much good for a Sumif.


Code:
Sum = Application.WorksheetFunction.SumIf(Transacties, Range("A2"), Transacties)

BTW. I don't think using Sum as a variable name is a good idea. :)
 
Last edited:

williamgowtama

Board Regular
Joined
Apr 12, 2015
Messages
123
MARK858's code is correct, i made a mistake in my code
Code:
[COLOR=#333333]Sum = Application.WorksheetFunction.SumIf([B]Range("Transacties")[/B], Range("A2"), (Range("Transacties")))[/COLOR]
it should be:
Code:
[COLOR=#333333]Sum = Application.WorksheetFunction.SumIf([B]Transacties[/B], Range("A2"), Transacties)[/COLOR]
Using Sum as variable is okay, but in some cases, like "mod", it can't be assigned as variable because its an identifier. so try to put numbering to the variable, for example sum1 which will make sure that the used variable wont causes unwanted errors in future uses.

For this part:
Code:
[COLOR=#333333][I]Range("A1").Value = "Subcategorie"[/I][/COLOR]
it'd be better to use this instead to prevent unwanted result:
Code:
[B][COLOR=#333333][FONT=Verdana]Sheets("[/FONT][/COLOR][FONT=Verdana]Som Transacties[/FONT][B][COLOR=#333333][FONT=Verdana]").[/FONT][/COLOR]Range("A1").Value = "Subcategorie"

Cheers[/B][/B]
 

hansgrandia

Board Regular
Joined
Jan 10, 2015
Messages
50

ADVERTISEMENT

Hello,

Thank you for the responses and apologies for the delay in my replay. I think I'm almost at the point of finalizing the code.
The code below only fills cell B2 (based on cell A2 (ANWB)).

What is the best approach to loop through all 20 categories (instead of just 1) and fill the B column with the correct values?
Loop until or For Each? Could you please support me in establishing the code?

Appreciated!
Hans


Sub Transacties2()


Dim TransSheet As Worksheet
Dim CalcSheet As Worksheet
Dim TransRange As Range
Dim CalcRange As Range
Dim SingleCell As Range
Dim Listofcells As Range
Dim TotalPerSub As Double


Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Som Transacties"


Set TransSheet = Sheets("Blad1")
Set CalcSheet = Sheets("Som Transacties")
Set TransRange = TransSheet.Range("D2:D120")
Set CalcRange = TransSheet.Range("G2:G120")


'Subcategories in Column A
CalcSheet.Range("A1").Value = "Subcategorie"
CalcSheet.Range("A2").Value = "ANWB"
CalcSheet.Range("A3").Value = "Autoverzekering"
CalcSheet.Range("A4").Value = "Bankkosten"
CalcSheet.Range("A5").Value = "Eten & drinken en persoonlijke verzorging"
CalcSheet.Range("A6").Value = "Kleding"
CalcSheet.Range("A7").Value = "Kranten / weekbladen / kerkblad / boeken"
CalcSheet.Range("A8").Value = "Lasten woning"
CalcSheet.Range("A9").Value = "Lidmaatschap kerk en goede doelen"
CalcSheet.Range("A10").Value = "Onderhoud auto"
CalcSheet.Range("A11").Value = "Opleiding en persoonlijke ontwikkeling"
CalcSheet.Range("A12").Value = "Overig"
CalcSheet.Range("A13").Value = "Reisverzekering"
CalcSheet.Range("A14").Value = "Sport"
CalcSheet.Range("A15").Value = "Uitvaartverzekering"
CalcSheet.Range("A16").Value = "Vakantie en ontspanning"
CalcSheet.Range("A17").Value = "Vakbond"
CalcSheet.Range("A18").Value = "Vervoer"
CalcSheet.Range("A19").Value = "Wegenbelasting"
CalcSheet.Range("A20").Value = "Zakgeld / cadeaus / boetes"
CalcSheet.Range("A21").Value = "Ziektenkosten"


'Select first blank cell of subcategory (Column B)
CalcSheet.Activate
Range("A2").Select


Do Until ActiveCell = Empty


ActiveCell.Offset(0, 1).Select


TotalPerSub = Application.WorksheetFunction.SumIf(TransRange, Range("A2"), CalcRange)
Range("B2").Value = TotalPerSub


' Move 1 row down
ActiveCell.Offset(1, -1).Select


Loop


Columns("A:A").EntireColumn.AutoFit


End Sub
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,140
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Try replacing

Code:
CalcSheet.Activate
 Range("A2").Select

Do Until ActiveCell = Empty
ActiveCell.Offset(0, 1).Select
TotalPerSub = Application.WorksheetFunction.SumIf(TransRange, Range("A2"), CalcRange)
Range("B2").Value = TotalPerSub
' Move 1 row down
 ActiveCell.Offset(1, -1).Select

Loop
Columns("A:A").EntireColumn.AutoFit

with
Code:
    With CalcSheet
        Dim cRng As Range

        For Each cRng In .Range("B2:B" & .Range("A" & Rows.Count).End(xlUp).Row)

            TotalPerSub = Application.WorksheetFunction.SumIf(TransRange, cRng.Offset(, -1), CalcRange)
            cRng.Value = TotalPerSub

        Next
        .Columns("A:A").EntireColumn.AutoFit
    End With
 

Forum statistics

Threads
1,136,322
Messages
5,675,083
Members
419,549
Latest member
EliteBeat

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
Top