Replicating INDIRECT within VBA

duffry

New Member
Joined
Mar 6, 2013
Messages
5
So I have a funky lookup formula that I use all over a sheet.


Code:
=SUMPRODUCT(SUMIFS(nameRange1,    nameRange2, A$1,    nameRange3,  INDIRECT($B$1)))

This let me build a report page with many of these style lookups, changing nameRange1 depending on the column of data I want in that cell.
nameRange2 is a date column in the data source, and cell B1 holds a reference to the name of the group of data I want (shoes, books etc).
I then could duplicate the worksheet and change the content of cell B1 and all my many formulas would find the stuff they need.
The references are on a separate settings sheet. Sometimes they refer to a single cell and other times to a whole table of data ("Table1[#Data]").

This all works just fine.

I'm now trying to replicate some of this function within VBA to save a JSON output of some of the data.
I want to continue to be able to administer the groupings of items (shoes in Table1, books in Table2 etc) in the same way but am struggling to reproduce the above formula in VBA.

I have gotten as far as:
Code:
    numThings = numThings & _
        Application.WorksheetFunction.SumProduct( _
            Application.WorksheetFunction.SumIfs( _
                Names("nameRange1").RefersToRange, _
                Names("nameRange2").RefersToRange, Names("reportDate").RefersToRange.Value + Names("intervalTimes").RefersToRange(1).Value, _
                Names("nameRange3").RefersToRange, Application.WorksheetFunction.indirect("Table1[#Data]") _
            ) _
        )

But it doesn't like it at all.
I have tested all other elements on their own and they work, it's just the indirect that fails (also doesn't swap in capital letters in the same way as SumIfs etc, so assuming it's not supported).

I played about with something like
Code:
Worksheets("Settings").ListObjects("Table1").DataBodyRange
But with similar results.

Is there any clue anyone can offer me to help?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
20,212
Office Version
  1. 365
Platform
  1. Windows
Try...

Code:
Dim MyCritRng As Variant
Dim MyResult As Double

Set MyCritRng = Evaluate("INDIRECT($B$1)")

MyResult = Evaluate("=SUMPRODUCT(SUMIFS(nameRange1,nameRange2,A$1,nameRange3," & MyCritRng.Address & "))")

Hope this helps!
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,485
Messages
5,831,964
Members
430,098
Latest member
bemmelen

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