VBA: Move declared sheet to the last sheet of another workbook

shane_aldrich

Board Regular
Joined
Oct 23, 2009
Messages
148
I have two workbooks open. After my active workbook shift 11 columns to the sheet "Import", I'm trying to move a copy of the import sheet to another workbook that's already opened named RYG_Raw. I'm not really sure where the problem is, but my code gets hung up when I try to set wb2

Sub Raw_Sales()
'*************************
'*** Declare Variables ***
'*************************
Dim t1 As Date
Dim sFileName As String
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
'*********************
'*** Set Variables ***
'*********************
t1 = Now
sFileName = "RYG_Raw"
Set wb1 = ThisWorkbook
Set wb2 = sFileName & "_" & Format(t1, "YYYY_MM_DD") & ".xls"
Set ws1 = Sheets("Sales_Compile")
Set ws2 = Sheets("Import")
Set rng1 = ws1.Range(ws1.Columns(1), ws1.Columns(11))

'***************
'*** Execute ***
'***************

rng1.Copy Destination:=ws2.Cells(1)
ws2.Copy After:=wb2.Sheets(wb2.Sheets.Count)
wb1.Activate
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
The code will give you error. You need to specify the object.
Code:
Set wb2 = sFileName & "_" & Format(t1, "YYYY_MM_DD") & ".xls"
should be:
Code:
Set wb2 = [COLOR=#0000ff]Workbooks([/COLOR]sFileName & "_" & Format(t1, "YYYY_MM_DD") & ".xls"[COLOR=#0000ff])[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,219,162
Messages
6,146,661
Members
450,706
Latest member
LGVBPP

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