Relative referral to cell on previous sheet

andrewvanmarle

New Member
Joined
Aug 11, 2015
Messages
40
Hi all,

I googled this first, found several sites citing the same VBA code:

VBA Code:
Function PrevSheet(rCell As Range)
    Application.Volatile
    Dim i As Integer
    i = rCell.Cells(1).Parent.Index
    PrevSheet = Sheets(i - 1).Range(rCell.Address)
End Function

And then enter this to the cell in question:

=PrevSheet(A1)


but I get no value....

I assume im doing something wrong??
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Have you enabled VBA in your workbook?
Are you trying to use this on some sheet other than the first sheet?

You should get something return (i.e. at least a 0 or a #VALUE error).
 
Upvote 0
Hi Joe,

Yeah VBA is enabled. And i'd use this on every sheet, referring to the previous one. (everysheet is a year and I want to transport the end value to the next....)
 
Upvote 0
And i'd use this on every sheet,
You cannot use it on the first sheet, as there is no previous sheet to the first sheet.

Where exactly have you placed this VBA code?
To make it accessible from ALL pages, you need to put it in a new, general module.
If you put it in one of the pre-defined sheet modules, it will only work on that sheet.
 
Upvote 0
If its next sheet, I would create a new similar UDF, i.e.
VBA Code:
Function NextSheet(rCell As Range)
    Application.Volatile
    Dim i As Integer
    i = rCell.Cells(1).Parent.Index
    NextSheet = Sheets(i + 1).Range(rCell.Address)
End Function
 
Upvote 0
By the way,

If I want to do this without VBA, would this idea work?

Sheet names are years, and A1 is manually filled in with the year of the sheet (ex 2020, 2021 etc)

Can I refer to a cell on a sheet by using the value on A1 minus one? essentially this:

Excel Formula:
='2020'!F2
Where 2020 would be a1-1.

I tried that but that made excel try to open a file (weird)
 
Upvote 0
You can use the INDIRECT function to do that.
Basically, you build the range reference formulaically, then surround it in INDIRECT, i.e.
Excel Formula:
=INDIRECT("'" & A1-1 & "'!F2")
 
Upvote 0
Solution

Forum statistics

Threads
1,224,828
Messages
6,181,213
Members
453,024
Latest member
Wingit77

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