Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 13
Default referring to previous worksheet in workbook

Back to my timesheet workbook...

I add a new sheet for each pay period (every two weeks). I name each
timesheet with the last date of the period (current sheet is 10.27.07;
previous one is 10.13.07).

To keep track of the amount of leave time accumulated I have to use cell
values from the previous periods timesheet. It is not a specific sheet; it
is relative, i.e. always the sheet preceeding the current one).

Currently, I manually change those cells to reflect the correct timesheet
name. Is there a way to refer to the relative timesheet in excel?


  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 4,624
Default referring to previous worksheet in workbook

See

http://mcgimpsey.com/excel/udfs/prevsheet.html

In article ,
Freida wrote:

Back to my timesheet workbook...

I add a new sheet for each pay period (every two weeks). I name each
timesheet with the last date of the period (current sheet is 10.27.07;
previous one is 10.13.07).

To keep track of the amount of leave time accumulated I have to use cell
values from the previous periods timesheet. It is not a specific sheet; it
is relative, i.e. always the sheet preceeding the current one).

Currently, I manually change those cells to reflect the correct timesheet
name. Is there a way to refer to the relative timesheet in excel?

  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 22,906
Default referring to previous worksheet in workbook

Depends upon how you are adding a new sheet.

Are you copying and clearing constants from the copied sheet or just
InsertWorksheet?

Do you have a worksheet template you use for inserting?

Post back and we'll come up with something automatic.

But here is a UDF that refers to the previous sheet.

Function PrevSheet(rg As Range)
n = Application.Caller.Parent.Index
If n = 1 Then
PrevSheet = CVErr(xlErrRef)
ElseIf TypeName(Sheets(n - 1)) = "Chart" Then
PrevSheet = CVErr(xlErrNA)
Else
PrevSheet = Sheets(n - 1).Range(rg.Address).Value
End If
End Function

In a cell enter =PrevSheet(A1) to return the contents of A1 from previous
sheet.

Copy/paste the UDF above into a General Module in your workbook.

If not familiar with macros and VBA, visit David McRitchie's website on
"getting started".

http://www.mvps.org/dmcritchie/excel/getstarted.htm

In the meantime..........

To create a General Module, hit ALT + F11 to open the Visual Basic Editor.

Hit CRTL + R to open Project Explorer.

Find your workbook/project and select it.

Right-click and InsertModule. Paste the above code in there. Save the
workbook and hit ALT + Q to return to your workbook.


Gord Dibben Excel MVP


On Mon, 22 Oct 2007 10:20:02 -0700, Freida
wrote:

Back to my timesheet workbook...

I add a new sheet for each pay period (every two weeks). I name each
timesheet with the last date of the period (current sheet is 10.27.07;
previous one is 10.13.07).

To keep track of the amount of leave time accumulated I have to use cell
values from the previous periods timesheet. It is not a specific sheet; it
is relative, i.e. always the sheet preceeding the current one).

Currently, I manually change those cells to reflect the correct timesheet
name. Is there a way to refer to the relative timesheet in excel?


  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 13
Default referring to previous worksheet in workbook

In answer to your questions:

Are you copying and clearing constants from the copied sheet or just
InsertWorksheet? Using InsertWorksheet...

Do you have a worksheet template you use for inserting? Template that I
made up.

As I mentionned in my first post about this timesheet, I am a self-taught
novice so bear with me while I make sure I understood properly so far:
I follow your directions to create the general module and then paste in the
code you gave me.
However, I think I was using the wrong terminology when I said "previous
sheet"..which it seems to me must refer to the one to the immediate left of
the current sheet. However I insert each new sheet on top of the previous
ones...which means I want to refer to the sheet to the immediate right of the
current one. Am I correct that I therefore have to replace all the n-1 with
n+1...and I probably also have to get rid of that error condition if n=1. So
would I be left with something like this:

Function PrevSheet(rg As Range)
n = Application.Caller.Parent.Index
If TypeName(Sheets(n + 1)) = "Chart" Then
PrevSheet = CVErr(xlErrNA)
Else
PrevSheet = Sheets(n + 1).Range(rg.Address).Value
End If
End Function

I am really swimming in strange waters here...so please advise if I am
correct.











  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 22,906
Default referring to previous worksheet in workbook

Changing the (n - 1) to (n + 1) seems to work to get data from sheet to right.

I would leave the error condition line, otherwise if there is no sheet to right
you will get a #VALUE! error in cell rather than the #REF! which may be
misleading.


Gord

On Mon, 22 Oct 2007 15:01:03 -0700, Freida
wrote:

In answer to your questions:

Are you copying and clearing constants from the copied sheet or just
InsertWorksheet? Using InsertWorksheet...

Do you have a worksheet template you use for inserting? Template that I
made up.

As I mentionned in my first post about this timesheet, I am a self-taught
novice so bear with me while I make sure I understood properly so far:
I follow your directions to create the general module and then paste in the
code you gave me.
However, I think I was using the wrong terminology when I said "previous
sheet"..which it seems to me must refer to the one to the immediate left of
the current sheet. However I insert each new sheet on top of the previous
ones...which means I want to refer to the sheet to the immediate right of the
current one. Am I correct that I therefore have to replace all the n-1 with
n+1...and I probably also have to get rid of that error condition if n=1. So
would I be left with something like this:

Function PrevSheet(rg As Range)
n = Application.Caller.Parent.Index
If TypeName(Sheets(n + 1)) = "Chart" Then
PrevSheet = CVErr(xlErrNA)
Else
PrevSheet = Sheets(n + 1).Range(rg.Address).Value
End If
End Function

I am really swimming in strange waters here...so please advise if I am
correct.













  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 13
Default referring to previous worksheet in workbook

I tried posting this earlier today but it has not shown up...so I am
reposting. If it is duplicate, I apologize.

I see your point about setting up something in the event of no sheet to the
right of the current sheet (which should only happen for the first sheet of
the year).

But since my current sheet is always on top, wouldn't the error of n= 1
always be triggered?

I was thinking of using a separate template for the first sheet of the year
(or just blanking out those fields in the first sheet)...but if there were a
way to determine if there was no sheet to the right of the current sheet, I
could use the If function to put the correct data in the first sheet
automatically. I would prefer this way, if it is possible.

"Gord Dibben" wrote:

Changing the (n - 1) to (n + 1) seems to work to get data from sheet to right.

I would leave the error condition line, otherwise if there is no sheet to right
you will get a #VALUE! error in cell rather than the #REF! which may be
misleading.


Gord

On Mon, 22 Oct 2007 15:01:03 -0700, Freida
wrote:

In answer to your questions:

Are you copying and clearing constants from the copied sheet or just
InsertWorksheet? Using InsertWorksheet...

Do you have a worksheet template you use for inserting? Template that I
made up.

As I mentionned in my first post about this timesheet, I am a self-taught
novice so bear with me while I make sure I understood properly so far:
I follow your directions to create the general module and then paste in the
code you gave me.
However, I think I was using the wrong terminology when I said "previous
sheet"..which it seems to me must refer to the one to the immediate left of
the current sheet. However I insert each new sheet on top of the previous
ones...which means I want to refer to the sheet to the immediate right of the
current one. Am I correct that I therefore have to replace all the n-1 with
n+1...and I probably also have to get rid of that error condition if n=1. So
would I be left with something like this:

Function PrevSheet(rg As Range)
n = Application.Caller.Parent.Index
If TypeName(Sheets(n + 1)) = "Chart" Then
PrevSheet = CVErr(xlErrNA)
Else
PrevSheet = Sheets(n + 1).Range(rg.Address).Value
End If
End Function

I am really swimming in strange waters here...so please advise if I am
correct.












  #7   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 22,906
Default referring to previous worksheet in workbook

but if there were a
way to determine if there was no sheet to the right of the current sheet, I
could use the If function to put the correct data in the first sheet
automatically


If no sheet to the right, and you had a formula of =PrevSheet(A1) from where
would the data come to be filled in automatically?


Gord

On Tue, 23 Oct 2007 08:59:00 -0700, Freida
wrote:

I tried posting this earlier today but it has not shown up...so I am
reposting. If it is duplicate, I apologize.

I see your point about setting up something in the event of no sheet to the
right of the current sheet (which should only happen for the first sheet of
the year).

But since my current sheet is always on top, wouldn't the error of n= 1
always be triggered?

I was thinking of using a separate template for the first sheet of the year
(or just blanking out those fields in the first sheet)...but if there were a
way to determine if there was no sheet to the right of the current sheet, I
could use the If function to put the correct data in the first sheet
automatically. I would prefer this way, if it is possible.

"Gord Dibben" wrote:

Changing the (n - 1) to (n + 1) seems to work to get data from sheet to right.

I would leave the error condition line, otherwise if there is no sheet to right
you will get a #VALUE! error in cell rather than the #REF! which may be
misleading.


Gord

On Mon, 22 Oct 2007 15:01:03 -0700, Freida
wrote:

In answer to your questions:

Are you copying and clearing constants from the copied sheet or just
InsertWorksheet? Using InsertWorksheet...

Do you have a worksheet template you use for inserting? Template that I
made up.

As I mentionned in my first post about this timesheet, I am a self-taught
novice so bear with me while I make sure I understood properly so far:
I follow your directions to create the general module and then paste in the
code you gave me.
However, I think I was using the wrong terminology when I said "previous
sheet"..which it seems to me must refer to the one to the immediate left of
the current sheet. However I insert each new sheet on top of the previous
ones...which means I want to refer to the sheet to the immediate right of the
current one. Am I correct that I therefore have to replace all the n-1 with
n+1...and I probably also have to get rid of that error condition if n=1. So
would I be left with something like this:

Function PrevSheet(rg As Range)
n = Application.Caller.Parent.Index
If TypeName(Sheets(n + 1)) = "Chart" Then
PrevSheet = CVErr(xlErrNA)
Else
PrevSheet = Sheets(n + 1).Range(rg.Address).Value
End If
End Function

I am really swimming in strange waters here...so please advise if I am
correct.













  #8   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 13
Default referring to previous worksheet in workbook

Because I would condition it with the If function. If there *was* a sheet
the cell would me =PrevSheet(A1) + 4.65; if there *was not* a sheet to the
right then =4.65

"Gord Dibben" wrote:

but if there were a
way to determine if there was no sheet to the right of the current sheet, I
could use the If function to put the correct data in the first sheet
automatically


If no sheet to the right, and you had a formula of =PrevSheet(A1) from where
would the data come to be filled in automatically?


Gord

On Tue, 23 Oct 2007 08:59:00 -0700, Freida
wrote:

I tried posting this earlier today but it has not shown up...so I am
reposting. If it is duplicate, I apologize.

I see your point about setting up something in the event of no sheet to the
right of the current sheet (which should only happen for the first sheet of
the year).

But since my current sheet is always on top, wouldn't the error of n= 1
always be triggered?

I was thinking of using a separate template for the first sheet of the year
(or just blanking out those fields in the first sheet)...but if there were a
way to determine if there was no sheet to the right of the current sheet, I
could use the If function to put the correct data in the first sheet
automatically. I would prefer this way, if it is possible.

"Gord Dibben" wrote:

Changing the (n - 1) to (n + 1) seems to work to get data from sheet to right.

I would leave the error condition line, otherwise if there is no sheet to right
you will get a #VALUE! error in cell rather than the #REF! which may be
misleading.


Gord

On Mon, 22 Oct 2007 15:01:03 -0700, Freida
wrote:

In answer to your questions:

Are you copying and clearing constants from the copied sheet or just
InsertWorksheet? Using InsertWorksheet...

Do you have a worksheet template you use for inserting? Template that I
made up.

As I mentionned in my first post about this timesheet, I am a self-taught
novice so bear with me while I make sure I understood properly so far:
I follow your directions to create the general module and then paste in the
code you gave me.
However, I think I was using the wrong terminology when I said "previous
sheet"..which it seems to me must refer to the one to the immediate left of
the current sheet. However I insert each new sheet on top of the previous
ones...which means I want to refer to the sheet to the immediate right of the
current one. Am I correct that I therefore have to replace all the n-1 with
n+1...and I probably also have to get rid of that error condition if n=1. So
would I be left with something like this:

Function PrevSheet(rg As Range)
n = Application.Caller.Parent.Index
If TypeName(Sheets(n + 1)) = "Chart" Then
PrevSheet = CVErr(xlErrNA)
Else
PrevSheet = Sheets(n + 1).Range(rg.Address).Value
End If
End Function

I am really swimming in strange waters here...so please advise if I am
correct.














  #9   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 3
Default referring to previous worksheet in workbook

I absolutly love this function, however I am having a problem that if I have
another workbook open at the same time, then it sometimes grabs data from
Sheets(n-1) from that workbook. I added Application.Volatile to the function
to have the function get the new data as soon as the pointed to cell changes.
Could this be what is causing the workbook confusion?

Function PrevSheet(rg As Range)
n = Application.Caller.Parent.Index
If n = 1 Then
PrevSheet = CVErr(xlErrRef)
ElseIf TypeName(Sheets(n - 1)) = "Chart" Then
PrevSheet = CVErr(xlErrNA)
Else
PrevSheet = Sheets(n - 1).Range(rg.Address).Value
End If
End Function


  #10   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 35,218
Default referring to previous worksheet in workbook

You have an active thread elsewhere.

Balkar wrote:

I absolutly love this function, however I am having a problem that if I have
another workbook open at the same time, then it sometimes grabs data from
Sheets(n-1) from that workbook. I added Application.Volatile to the function
to have the function get the new data as soon as the pointed to cell changes.
Could this be what is causing the workbook confusion?

Function PrevSheet(rg As Range)
n = Application.Caller.Parent.Index
If n = 1 Then
PrevSheet = CVErr(xlErrRef)
ElseIf TypeName(Sheets(n - 1)) = "Chart" Then
PrevSheet = CVErr(xlErrNA)
Else
PrevSheet = Sheets(n - 1).Range(rg.Address).Value
End If
End Function


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
When I load a previous Excel workbook I lose my worksheet tabs. drk Excel Worksheet Functions 3 March 12th 07 05:19 PM
Referring to the previous selected sheet in a macro michaelberrier Excel Discussion (Misc queries) 2 June 12th 06 01:35 PM
Referring to function in another workbook psp Excel Worksheet Functions 1 August 31st 05 10:32 PM
Referring to Previous Worksheet - coding Denis Excel Worksheet Functions 7 December 10th 04 05:24 PM
referring to previous sheet Iolao Excel Discussion (Misc queries) 3 November 26th 04 08:40 PM


All times are GMT +1. The time now is 02:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"