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

kidneythief

New Member
Joined
Mar 17, 2021
Messages
34
Office Version
  1. 365
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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
It would be something like:

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

where A2 contains the product name of interest.
 
Upvote 0
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!
 
Upvote 0
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?
 
Upvote 0
Not really. Anything that uses INDIRECT is inherently clunky to be honest. ;)
 
Upvote 0

Forum statistics

Threads
1,214,535
Messages
6,120,090
Members
448,944
Latest member
sharmarick

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