Need help in filtering and then using sumproduct through VBA

Karter

New Member
Joined
Aug 20, 2014
Messages
1
Hi everyone

I'm trying to generate a report wherein i dump a relatively large dataset in a hidden sheet. To create the summary from this, I need to use a criteria on a one field and then sumproduct the visible records from two others

For illustration, consider the following table:

Scenario : 1,1,1,2,2,3,3,3,3
Name: a,b,c,d,f,g,h,a,d
Met1: 5,3,5,4,2,7,9,7,1
Met2: 0.5, 0.1,1.0,0.2,1.1,0.3,1.3,0.5,0.9


I need to filter the table on Scenario (say 2) and then sumproduct(met1,met2) to populate a cell via VBA code. I'm fairly new to the language so any help would be much appreciated.

Thanks.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
the data should be like this in sheet

Sheet1

*ABCD
1Scenario : 1Name:Met1:Met2:
21a50.5
31b30.1
41c51
52d40.2
62f21.1
73g70.3
83h91.3
93a70.5
103d10.9

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:64px;"><col style="width:64px;"><col style="width:64px;"><col style="width:64px;"></colgroup><tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4


now try this macro and see below data


Code:
Sub test()
Dim r As Range, filt As Range, met1 As Double, met2 As Double, dest As Range
Dim scenario As Integer, destdata As Range
Set dest = Range("A1").End(xlDown).Offset(5, 0)
Set r = Range("A1").CurrentRegion
dest = "scenario": dest.Offset(0, 1) = "met1": dest.Offset(0, 2) = "met2"
scenario = InputBox("type the number within columns A e.g. 2")
r.AutoFilter field:=1, Criteria1:=scenario
Set filt = r.Offset(1, 0).Resize(r.Rows.Count - 1)
met1 = WorksheetFunction.SumProduct(filt.Columns("A:A").SpecialCells(xlCellTypeVisible), filt.Columns("c:C").SpecialCells(xlCellTypeVisible))
MsgBox met1
met2 = WorksheetFunction.SumProduct(filt.Columns("A:A").SpecialCells(xlCellTypeVisible), filt.Columns("D:D").SpecialCells(xlCellTypeVisible))
MsgBox met2
Set destdata = dest.Offset(1, 0)
destdata = scenario
destdata.Offset(0, 1) = met1
destdata.Offset(0, 2) = met2
ActiveSheet.AutoFilterMode = False




End Sub
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,792
Members
449,468
Latest member
AGreen17

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