Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Denis
 
Posts: n/a
Default Referring to Previous Worksheet - coding

Hello,

I want to use code to programatically copy cells "A3"
and "D7" from WorksheetA to cells "B12" and "D4"
respectively in WorksheetB.

The catch is that WorksheetA will be reproduced regularly,
while "B12" and "D4" on WorksheetB will be dependent onthe
selected, reproduced WorksheetA.

My biggest difficulty (I think) is to refer back from
WorksheetB to the previous sheet from which the 2nd 'copy'
operation took place.

Any assistance would be greatly appreciated.

Thank you Denis
  #2   Report Post  
Frank Kabel
 
Posts: n/a
Default

Hi
ee:
http://www.dicks-blog.com/archives/2...o-previous-she
et/trackback/

--
Regards
Frank Kabel
Frankfurt, Germany

"Denis" schrieb im Newsbeitrag
...
Hello,

I want to use code to programatically copy cells "A3"
and "D7" from WorksheetA to cells "B12" and "D4"
respectively in WorksheetB.

The catch is that WorksheetA will be reproduced regularly,
while "B12" and "D4" on WorksheetB will be dependent onthe
selected, reproduced WorksheetA.

My biggest difficulty (I think) is to refer back from
WorksheetB to the previous sheet from which the 2nd 'copy'
operation took place.

Any assistance would be greatly appreciated.

Thank you Denis


  #3   Report Post  
Denis
 
Posts: n/a
Default

Thank you Frank, although the worksheets, from where the
code will originate from, may not necessarily be adjacent
to the WorksheetB, you have given me something to start
with.
Thanks again

Denis
-----Original Message-----
Hi
ee:
http://www.dicks-blog.com/archives/2.../02/referring-

to-previous-she
et/trackback/

--
Regards
Frank Kabel
Frankfurt, Germany

"Denis" schrieb im

Newsbeitrag
...
Hello,

I want to use code to programatically copy cells "A3"
and "D7" from WorksheetA to cells "B12" and "D4"
respectively in WorksheetB.

The catch is that WorksheetA will be reproduced

regularly,
while "B12" and "D4" on WorksheetB will be dependent

onthe
selected, reproduced WorksheetA.

My biggest difficulty (I think) is to refer back from
WorksheetB to the previous sheet from which the

2nd 'copy'
operation took place.

Any assistance would be greatly appreciated.

Thank you Denis


.

  #4   Report Post  
Gord Dibben
 
Posts: n/a
Default

Denis

Copy/paste this User Defined Function to a general module in your workbook.

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

'Enter =PrevSheet(B2) on sheet2 and you'll get B2 from sheet1.

OR call the Function in your code.

Gord Dibben Excel MVP

On Thu, 9 Dec 2004 12:28:59 -0800, "Denis"
wrote:

Hello,

I want to use code to programatically copy cells "A3"
and "D7" from WorksheetA to cells "B12" and "D4"
respectively in WorksheetB.

The catch is that WorksheetA will be reproduced regularly,
while "B12" and "D4" on WorksheetB will be dependent onthe
selected, reproduced WorksheetA.

My biggest difficulty (I think) is to refer back from
WorksheetB to the previous sheet from which the 2nd 'copy'
operation took place.

Any assistance would be greatly appreciated.

Thank you Denis


  #5   Report Post  
Denis
 
Posts: n/a
Default


Thank you Gord, unfortunately I tried to figure it out
and modify it somewhat, but admittedly, it was a little
above my head.

I have managed to come up with the following code. Not
very pretty nor sleek, but it's doing what I was looking
for.

Cheers!

Sub CopyToSecond()
'
' CopyToSecond Macro
' Macro recorded 12/9/2004 by Denis Bisson
'
If ActiveSheet.Name = "Second" Then ' if this sheet is
named 'Second' then exit the subroutine
Exit Sub
Else
'
Range("A3").Select 'select and paste the first cell
(cell to be determined)
Selection.Copy
Range("C19").Select 'cell number may need to be
changed - NOTE lock cell!
ActiveSheet.Paste
Application.CutCopyMode = False

Range("C3").Select 'select and paste the second cell
(cell to be determined)
Selection.Copy
Range("D19").Select 'cell number may need to be
changed - NOTE lock cell!
ActiveSheet.Paste
Application.CutCopyMode = False

' The value of the two copied cells are now pasted in
adjacent cells.
' Now, both cells will be copied; any
lines/formatting will be removed;
' the font will be changed to white (we don't want to
see the text);and
' both cells will be copied


Range("C19:D19").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle =
xlNone
Selection.Font.ColorIndex = 2 'White
Selection.Copy

' Now the 'Second' sheet is selected; two adjacent
cells are selected; and
' the values (as well as any formatting) from the
previous sheet are pasted
Sheets("Second").Select
Range("C19:D19").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1").Select 'set the focus to cell A1
End If
End Sub


  #6   Report Post  
Gord Dibben
 
Posts: n/a
Default

Denis

Your code can be cleaned up somewhat.......very rarely do you need to "select"
objects such as ranges and sheets.

Sub CopyToSecond()

If ActiveSheet.Name = "Second" Then
Exit Sub
Else
Range("C19").Value = Range("A3").Value
Range("D19").Value = Range("C3").Value
With Range("C19:D19")
.ClearFormats
.Font.ColorIndex = 2 'White
.Copy Destination:=Sheets("Second").Range("C19")
End With
Range("A1").Select 'set the focus to cell A1

End If

End Sub

Your comments on "cell to be determined" lead me to believe that you would be
having some sort of user input.

Check out the use of InputBox and Application.InputBox for choosing cells.

Gord


On Thu, 9 Dec 2004 16:53:41 -0800, "Denis"
wrote:


Thank you Gord, unfortunately I tried to figure it out
and modify it somewhat, but admittedly, it was a little
above my head.

I have managed to come up with the following code. Not
very pretty nor sleek, but it's doing what I was looking
for.

Cheers!

Sub CopyToSecond()
'
' CopyToSecond Macro
' Macro recorded 12/9/2004 by Denis Bisson
'
If ActiveSheet.Name = "Second" Then ' if this sheet is
named 'Second' then exit the subroutine
Exit Sub
Else
'
Range("A3").Select 'select and paste the first cell
(cell to be determined)
Selection.Copy
Range("C19").Select 'cell number may need to be
changed - NOTE lock cell!
ActiveSheet.Paste
Application.CutCopyMode = False

Range("C3").Select 'select and paste the second cell
(cell to be determined)
Selection.Copy
Range("D19").Select 'cell number may need to be
changed - NOTE lock cell!
ActiveSheet.Paste
Application.CutCopyMode = False

' The value of the two copied cells are now pasted in
adjacent cells.
' Now, both cells will be copied; any
lines/formatting will be removed;
' the font will be changed to white (we don't want to
see the text);and
' both cells will be copied


Range("C19:D19").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle =
xlNone
Selection.Font.ColorIndex = 2 'White
Selection.Copy

' Now the 'Second' sheet is selected; two adjacent
cells are selected; and
' the values (as well as any formatting) from the
previous sheet are pasted
Sheets("Second").Select
Range("C19:D19").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1").Select 'set the focus to cell A1
End If
End Sub


  #7   Report Post  
Denis
 
Posts: n/a
Default

Gord,
Yes indeed, there was much potential for cleaner code
(quite new at this) and I appreciate your help in this
matter. I learned something new today - life is good!

Oh, the user input thing.... should have been removed from
my code for the posting... it was simply a reminder for
me - the cells will be different.

Cheers!
Denis
  #8   Report Post  
Gord Dibben
 
Posts: n/a
Default

Thanks for the feedback Denis.

Gord

On Fri, 10 Dec 2004 05:24:57 -0800, "Denis"
wrote:

Gord,
Yes indeed, there was much potential for cleaner code
(quite new at this) and I appreciate your help in this
matter. I learned something new today - life is good!

Oh, the user input thing.... should have been removed from
my code for the posting... it was simply a reminder for
me - the cells will be different.

Cheers!
Denis


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
copyright and worksheet protection dow Excel Discussion (Misc queries) 2 January 3rd 05 03:07 PM
Executing macro for all worksheet from a different worksheet Biti New Users to Excel 3 December 8th 04 10:05 AM
Reference Data in Moved Worksheet tommcbrny Setting up and Configuration of Excel 1 December 1st 04 06:49 PM
Worksheet name and Backward compatibility Rich Excel Discussion (Misc queries) 3 November 30th 04 06:10 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 01:34 AM.

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"