Referencing a sheet by its order or index number in a formula

kidneythief

New Member
Joined
Mar 17, 2021
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
Hi! I'm trying to insert a sheet reference within a countif formula based on the sheet's index number.
My sales sheet (sheet 3) contains a list of items sold in column G. A separate sheet is used per month so the current active sheet would be named APRIL 2021 with the newest, active sheet always being the third from the left.
My invoice sheet (sheet 2) contains a full product list. I'm trying to set up a column here that counts the number of instances of each product list entry that is found in the active sales sheet (always sheet 3).

I've added this module for returning the sheet names:

Function SHEETNAME(number As Long) As String
SHEETNAME = Sheets(number).Name
End Function

Apart from that, I'm having trouble putting together the COUNTIF function (with maybe INDIRECT??) without returning VALUE, NAME, or REF errors.

Any help would be greatly appreciated!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,103
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
It would be something like:

Excel Formula:
=COUNTIF(INDIRECT("'"&SHEETNAME(3)&"'!G:G"),A2)

where A2 contains the product name of interest.
 

kidneythief

New Member
Joined
Mar 17, 2021
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
It would be something like:

Excel Formula:
=COUNTIF(INDIRECT("'"&SHEETNAME(3)&"'!G:G"),A2)

where A2 contains the product name of interest.
Ah thank you, I had not been properly replacing the variable in the place of A2! Thanks for the help, Rory!
 

kidneythief

New Member
Joined
Mar 17, 2021
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
It would be something like:

Excel Formula:
=COUNTIF(INDIRECT("'"&SHEETNAME(3)&"'!G:G"),A2)

where A2 contains the product name of interest.
Ah, it turns out I had to add up values in a quantity column next to each instance of the product name (e.g. in the quantity column next to one instance of Product A it might say 20 packs, in another instance 30 packs, and I need the sum of those values, or 50)

I've come up with this, which seems to work, but looks quite clunky:

=SUMIF(INDIRECT("'"&SHEETNAME(3)&"'!G:G"),A2,(INDIRECT("'"&SHEETNAME(3)&"'!Z:Z")))

where G:G is the Product List Column, A2 is the item name, and Z:Z is the Quantity column.

Is there a better way to do this?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,103
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Not really. Anything that uses INDIRECT is inherently clunky to be honest. ;)
 

Forum statistics

Threads
1,136,303
Messages
5,674,968
Members
419,537
Latest member
ucatchy

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