[VBA] Basic Sumif function usage in VBA

Alexlin

New Member
Joined
Sep 29, 2018
Messages
15
Thanks for visiting mypost. I have encountered a difficulty in applying sumif function.

The raw data is likethis.

In worksheet 1, thereare 100 companies' name on one row and 87 expense items on one column
Worksheet 2 is atemplate. I need to create a worksheet for each entity based on worksheet 2.


So I have the followingcode. For creating new tab, it is fine. For applying the sumif function, Ifailed. What I need is to get the raw data from worksheet 1 to worksheet 2.
Can any expert help tomodify the code below? I do not understand why it's not working.

Dim i As Long, entity As Variant

For i = 1 To 100

Worksheets("ws1").Activate
Cells(2, i).Activate
Set entity = ActiveCell
'Creating new tab foreach company and change the tab name
Worksheets("ws2").Copyafter:=Worksheets(Worksheets.Count)
Worksheets("ws2").name =RTrim(Left(entity, 31))
'Apply sumif function(But I dunno how why it doesnt work, and I think I can apply For_Next hereagain)

Range("D8") =Application.SumIf(Worksheets("ws1").Columns("A"),Range("G8"), Worksheets("ws1").Cells(2, i).Columns)

Range("D9") =Application.SumIf(Worksheets("ws1").Columns("A"),Range("G9"), Worksheets("ws1").Cells(2, i).Columns)

Range("D10") = Application.SumIf(Worksheets("ws1").Columns("A"),Range("G10") Worksheets("ws1").Cells(2, i).Columns)

....

Range("D87") =Application.SumIf(Worksheets("ws1").Columns("A"),Range("G87"), Cells(2, i).Columns)

Next

Thanks v. much!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Usually the first argument of a SUMIF is a range and the second is the criteria.

In your code you appear to be using a single cell for the first argument and the second argument.

What range(s) do you want to sum and what criteria do you want to use?
 
Upvote 0
Usually the first argument of a SUMIF is a range and the second is the criteria.

In your code you appear to be using a single cell for the first argument and the second argument.

What range(s) do you want to sum and what criteria do you want to use?

Hi Norie, thanks for your replies. I have fixed the problem. However, I have onemore advanced question abt the sumif function.
You know, when we apply the sumif function in VBA, we canget the value directly rather than showing the formula.
If we wanna show the formula, we needa type sth likeRange("A1") = "=sumif(xxxxx,xxxx,xxxx)".
However, is it possible for VBA to show the formula if thesum range is variable.
Like on worksheet 1, it is Sumif(=SUMIF(F:F,H15,G:G)). On worksheet 2 it is Sumif(=SUMIF(F:F,H15,H:H))

I have the following code and it gets the value correctly.Is it possible to show the formula (Rather than value) correctly if the sumcolumn is variable?
For i = 4 To 100
Cells(2, i).Activate
AC = ActiveCell.Column
Range("D8") =Application.SumIf(Worksheets("Source").Columns("B"),Range("G8"), Worksheets("Source").Columns(AC))
Next

 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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