View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
JLatham JLatham is offline
external usenet poster
 
Posts: 3,365
Default Generic reference in a Macro

You'll need two variables in your recorded macro. Put something like this up
near the top of the Sub:

Const SourceSheet = "Feb" ' change to actual sheet name
Const DestSheet = "Mar" ' change to actual sheet name

now look through the code that was recorded and where you see entries like
Worksheets("Feb").Select
change those to
Worksheets(SourceSheet).Select
i.e., replacing "Feb" with SourceSheet
and similarly replacing "Mar" with DestSheet
Note that neither SourceSheet nor DestSheet have the quote marks around them.

For now you'll have to change the code each month, although you could set
those two constants up as variables and actually ask for the sheet names if
you wanted to. Again define the variables to be used up near the start of
the code:

Dim SourceSheet as String
Dim DestSheet as String

SourceSheet = InputBox("Enter Source Sheet Name", "Copy From", "")
If SourceSheet = "" Then
Exit Sub
End If
DestSheet = InputBox("Enter Destination Sheet Name", "Copy To", "")
If DestSheet = "" Then
Exit Sub
End If
and again change those current references to "Feb" and "Mar" to SourceSheet
and DestSheet.

To get into the macro you recorded to edit it, use
Tools | Macro | Macros then highlight the recorded macro and hit the [Edit]
button.

"Harry's GMail World" wrote:

Would like to have a macro to transfer data from one sheet to the new
one.

I have a Feb sheet with data and want to transfer data to the NEW Mar
sheet.

Problem is that to do a MACRO for Mar from Feb-which works- would not
work in Apr to take data from Mar because the macro only sees for the
Mar from Feb named worksheets/macro.

Need to change the reference (previous month) to a generic
recognizable by the MACRO for all months.