VBA, Sumifs, Long

Akbarov

Active Member
Joined
Jun 30, 2018
Messages
347
Office Version
  1. 365
Platform
  1. Windows
Hello Dear Community,

Can anyone help me please? what am i doing wrong here?
IfCell and IsCell are sport names
SumCell is scores they achieved

VBA Code:
Sub test()
  Dim IfCell As Range, lr As Long
  Dim IsCell As Range
  Dim SumCell As Range, lr1 As Long
  Dim ResultCell As Range
  Dim CriteriaValue As String
  lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  lr1 = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  Set IfCell = Sheets("DATA").Range("ae25:ae2000")
  Set IsCell = Sheets("HISTOR").Range("F41")
  Set SumCell = Sheets("DATA").Range("AF25:AF2000")
  Set ResultCell = Sheets("HISTOR").Range("J41")

  CriteriaValue = "<>"

  ResultCell = WorksheetFunction.SumIfs(SumCell, IfCell, IsCell)
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi Akbarov,

Kindly share the sheet having data and what is expected from the code.

Thanks,
Saurabh
 
Upvote 0

Hello, Saurabhj

It is not complicated, Sumifs:
IfCell = Sheets("DATA").Range("ae25:ae2000") List Of Sports ( 16 different sports )
match
IsCell = Sheets("HISTOR").Range("F41") <--- Name of sport i am trying to sum results
Sum = Sheets("DATA").Range("AF25:AF2000") <--- This column is result of scores they achieved so they are numbers ( but formulas )

Instead of using range like AF25:AF2000 i want to use last.row but i forgot how to do that. ("AF25:AF") & lastrow

And finally i want result of calculation here:
ResultCell = Sheets("HISTOR").Range("J41")
 
Upvote 0
VBA Code:
Sub test()
  Dim IfCell As Range, lr As Long
  Dim IsCell As Range
  Dim SumCell As Range, lr1 As Long
  Dim ResultCell As Range
  Dim CriteriaValue As String
  lr = Sheets("DATA").Cells(Rows.Count, 31).End(xlUp).Row

  Set IfCell = Sheets("DATA").Range("AE25:AE" & lr)
  Set IsCell = Sheets("HISTOR").Range("F41")
  Set SumCell = Sheets("DATA").Range("AF25:AF" & lr)
  Set ResultCell = Sheets("HISTOR").Range("J41")

  CriteriaValue = "<>"

  ResultCell = WorksheetFunction.SumIfs(SumCell, IfCell, IsCell)
End Sub
 
Upvote 0
Thanks for reply but i get error Type-mismatch : ResultCell = WorksheetFunction.SumIfs
 
Upvote 0
Made little change it seems to work now, thank you very much.
2 question ,
what is Rows.Count,31 <---- 31 is id of column?
Do i need to change Dim as Range or other types if i will use this VBA for non-numerical data?
VBA Code:
Sub test()
  Dim IfCell As Range, lr As Long
  Dim IsCell As Range
  Dim SumCell As Range, lr1 As Long
  Dim ResultCell As Range
  lr = Sheets("DATA").Cells(Rows.Count, 31).End(xlUp).Row
  lr1 = Sheets("DATA").Cells(Rows.Count, 31).End(xlUp).Row
  Set IfCell = Sheets("DATA").Range("AE25:AE" & lr)
  Set IsCell = Sheets("HISTOR").Range("F41")
  Set SumCell = Sheets("DATA").Range("AF25:AF" & lr1)
  Set ResultCell = Sheets("HISTOR").Range("J41")

  CriteriaValue = "<>"

  ResultCell = WorksheetFunction.SumIfs(SumCell, IfCell, IsCell)
End Sub
 
Upvote 0
1. 31 is column number . for AE column number is 31
2. No. Range is Range don't different for Values or Stirngs ( Numbers or Text)
 
Upvote 0
I guess there is no any easy way to make this code to calculate from F1 to F45 and put results to J1 to J45
IsCell = Sheets("HISTOR").Range("F41")
Set ResultCell = Sheets("HISTOR").Range("J41")
 
Upvote 0
VBA Code:
Sub test()
  Dim IfCell As Range, lr As Long
  Dim IsCell As Range
  Dim SumCell As Range, lr1 As Long
  Dim ResultCell As Range, Cell As Range
  lr = Sheets("DATA").Cells(Rows.Count, 31).End(xlUp).Row
  lr1 = Sheets("DATA").Cells(Rows.Count, 32).End(xlUp).Row
  Set IfCell = Sheets("DATA").Range("AE25:AE" & lr)
Set SumCell = Sheets("DATA").Range("AF25:AF" & lr1)
  For Each Cell in Sheets("HISTOR").Range("F1:F45")
   Set IsCell = Cell
   Set ResultCell = Cell.offset(0, 4)
  'CriteriaValue = "<>"
  ResultCell = WorksheetFunction.SumIfs(SumCell, IfCell, IsCell)
Next Cell
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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