Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
KDJ KDJ is offline
external usenet poster
 
Posts: 19
Default Reference the previous Worksheet in a Formula

Hello
The following code is supposed to always reference the same cell in the
previous worksheet. Unfortunately, I can't figure out the sheet name with the
index number for the previous sheet. The line

ActiveCell.FormulaR1C1 = "=Sheets(x-1)!RC+2"

doesn't work.

Any suggestions?

Dim WeekStep As Integer
Dim TotalSheets As Integer
Dim x As Integer

WeekStep = InputBox("Number of weeks per worksheet:")
TotalSheets = InputBox("Number of Worksheets:")

For x = 2 To TotalSheets
Sheets(x).Select
Range("A5").Select
ActiveCell.FormulaR1C1 = "=Sheets(x-1)!RC+2"
Next x
--
Thanks very much. KDJ
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Reference the previous Worksheet in a Formula

When you use the .FormulaR1C1 property, you are directly setting the cell's
formula to the text within the quotes. This means that any
variables/methods/properties used in VBA (such as x in this example). You
must convert these to text that makes sense in a worksheet formula, so:
ActiveCell.FormulaR1C1 = "=Sheets(x-1)!RC+2"
becomes:
ActiveCell.FormulaR1C1 = "=" & Sheets(x-1).Name & "!RC+2"

Note also that you do not have to select a worksheet or cell in VBA before
you can manipulate it, so your code can be replace simply with :

For x = 2 To TotalSheets
Sheets(x).Range("A5").FormulaR1C1 = "=Sheets(x-1)!RC+2"
Next x

Cheers,
Dave

"KDJ" wrote:

Hello
The following code is supposed to always reference the same cell in the
previous worksheet. Unfortunately, I can't figure out the sheet name with the
index number for the previous sheet. The line

ActiveCell.FormulaR1C1 = "=Sheets(x-1)!RC+2"

doesn't work.

Any suggestions?

Dim WeekStep As Integer
Dim TotalSheets As Integer
Dim x As Integer

WeekStep = InputBox("Number of weeks per worksheet:")
TotalSheets = InputBox("Number of Worksheets:")

For x = 2 To TotalSheets
Sheets(x).Select
Range("A5").Select
ActiveCell.FormulaR1C1 = "=Sheets(x-1)!RC+2"
Next x
--
Thanks very much. KDJ

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Reference the previous Worksheet in a Formula

Try

For x = 2 To TotalSheet
Sheets(x).Range("A5").FormulaR1C1 = "=" & Sheets(x - 1).Name & "!RC+2"
Next x

--
Jacob


"KDJ" wrote:

Hello
The following code is supposed to always reference the same cell in the
previous worksheet. Unfortunately, I can't figure out the sheet name with the
index number for the previous sheet. The line

ActiveCell.FormulaR1C1 = "=Sheets(x-1)!RC+2"

doesn't work.

Any suggestions?

Dim WeekStep As Integer
Dim TotalSheets As Integer
Dim x As Integer

WeekStep = InputBox("Number of weeks per worksheet:")
TotalSheets = InputBox("Number of Worksheets:")

For x = 2 To TotalSheets
Sheets(x).Select
Range("A5").Select
ActiveCell.FormulaR1C1 = "=Sheets(x-1)!RC+2"
Next x
--
Thanks very much. KDJ

  #4   Report Post  
Posted to microsoft.public.excel.programming
KDJ KDJ is offline
external usenet poster
 
Posts: 19
Default Reference the previous Worksheet in a Formula

Thank you to both of you.
KDJ

"Jacob Skaria" wrote:

Try

For x = 2 To TotalSheet
Sheets(x).Range("A5").FormulaR1C1 = "=" & Sheets(x - 1).Name & "!RC+2"
Next x

--
Jacob


"KDJ" wrote:

Hello
The following code is supposed to always reference the same cell in the
previous worksheet. Unfortunately, I can't figure out the sheet name with the
index number for the previous sheet. The line

ActiveCell.FormulaR1C1 = "=Sheets(x-1)!RC+2"

doesn't work.

Any suggestions?

Dim WeekStep As Integer
Dim TotalSheets As Integer
Dim x As Integer

WeekStep = InputBox("Number of weeks per worksheet:")
TotalSheets = InputBox("Number of Worksheets:")

For x = 2 To TotalSheets
Sheets(x).Select
Range("A5").Select
ActiveCell.FormulaR1C1 = "=Sheets(x-1)!RC+2"
Next x
--
Thanks very much. KDJ

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
Reference previous worksheet within VLOOKUP Alex Mackenzie Excel Worksheet Functions 2 April 17th 09 07:20 PM
formula to reference cell on previous worksheet endless_thoughts17 Excel Worksheet Functions 2 January 25th 08 07:37 PM
need formula to reference a cell in previous worksheet Jim Excel Worksheet Functions 3 January 5th 07 04:05 AM
How to reference to the previous worksheet regardless of title? SeabeePlumber Excel Worksheet Functions 0 July 15th 05 11:05 AM
reference to previous worksheet Daniel Lidström Excel Programming 2 December 16th 03 07:34 PM


All times are GMT +1. The time now is 02:27 AM.

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

About Us

"It's about Microsoft Excel"