Macro Editing

christipper

New Member
Joined
Jun 14, 2011
Messages
26
HI,
I have recorded a macro to move data from one spreadsheet to specific cells in another, this all works fine for me.
My issue is that i want this to be done on a monthly basis but one of the file names will change each month and the original dat will be on a different row each month.
To give you a better idea. I will have sales data for several months in one file, at present i am down to row 135 which is for March 2013, the macro will currently copy this data and paste it to specific cells within the March accounts. However when we move into April i will now want the macro to pick up off row 136 and paste it to a completley different file, the April accounts.
This is the first part of the Macro that i have created.
Code:
Sub Sales_Summary()
'
' Sales_Summary Macro
' To transfer sales from Sales Analysis v5 to the Sales Summary tab
'
'
    Windows("Sales Analysis v5.xlsx").Activate
    Range("T135:U135").Select
    Selection.Copy
    Windows("Data 1303.xlsm").Activate
    Range("C10").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True
    Windows("Sales Analysis v5.xlsx").Activate
    Range("V135:W135").Select
    Selection.Copy
    Windows("Data 1303.xlsm").Activate
    Range("C13").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True
    Windows("Sales Analysis v5.xlsx").Activate
    Range("X135:Y135").Select
    Selection.Copy
    Windows("Data 1303.xlsm").Activate
    Range("C18").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True
End Sub
Any ideas?
Cheers
Chris
 
Last edited by a moderator:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Windows("Sales Analysis v5.xlsx").Activate <- activate the workbook to copy from
Range("T135:U135").Select <- specify your range to copy
Windows("Data 1303.xlsm").Activate <- activate the workbook to paste to

the rest is self-explanatory
 
Upvote 0
Any danger of some help, rather than pointing out that the system duplicated my posts, and that I've posted to more than one forum?
 
Upvote 0
Windows("Sales Analysis v5.xlsx").Activate <- activate the workbook to copy from
Range("T135:U135").Select <- specify your range to copy
Windows("Data 1303.xlsm").Activate <- activate the workbook to paste to

the rest is self-explanatory

The problem i'll mehowski is that next month the file "Data 1303.xlsm" will be "Data 1304.xlsm". Will this automatically change with the naming of the new workbook? If not how do i get the macro to auto change?

Chris
 
Upvote 0
Assuming that the code will be run in the correct month, try this:
Code:
Sub Sales_Summary()'
' Sales_Summary Macro
' To transfer sales from Sales Analysis v5 to the Sales Summary tab
'
'
   Dim wbSource               As Workbook
   Dim wbDest                 As Workbook
   Dim CopyRow                As Long


   Set wbSource = Workbook("Sales Analysis v5.xlsx")
   Set wbDest = Workbooks("Data " & Format(Date, "yymm") & ".xlsm")


   With wbSource.ActiveSheet
      ' find last used row in column T
      CopyRow = .Cells(.Rows.Count, "T").End(xlUp).Row
      .Cells(CopyRow, "T").Resize(1, 2).Copy
      wbDest.ActiveSheet.Range("C10").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                                            :=False, Transpose:=True
      .Cells(CopyRow, "V").Resize(, 2).Copy
      wbDest.ActiveSheet.Range("C13").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                                            :=False, Transpose:=True
      .Cells(CopyRow, "X").Resize(, 2).Copy
      wbDest.ActiveSheet.Range("C18").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                                            :=False, Transpose:=True
   End With
End Sub
 
Upvote 0
Many thanks Rory.

When i have tried to test this for April it is throwing back an error the wbDest statement. Can this only be run in April? Might sound like a stupid question but when you hover the mouse over the date part of the statement it displays todays date.

Chris
 
Upvote 0
Yes it can only be run in April (that's what I meant in the first part of my post) because it refers to today's date. You didn't really give a lot to go on so I had to make assumptions.
In truth, I would probably prefer to pop up a dialog to choose which target file to open and then just use that.
 
Upvote 0

Forum statistics

Threads
1,215,374
Messages
6,124,574
Members
449,173
Latest member
Kon123

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