Using VBA to copy data into a closed workbook?????

andrewrgd

Board Regular
Joined
Dec 30, 2004
Messages
78
I am trying to copy and export sheet1 from an open workbook to another specific closed workbook ie. c:\my documents\test.xls. There will also be several exports so I need the test workbook to keep excepting the data.

Ideally I will create a command button to run the code in the open workbook that I'm exporting.

Cheers
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

andrewrgd

Board Regular
Joined
Dec 30, 2004
Messages
78
Please Help!

I have used the following code to copy the required data into an archieve file. Is it possible to amend the code so instead of going into a separate workbook each time it goes into the same workbook but different sheet?

Thanks!

Sub CopyToMasterFile()
Dim Wb As Workbook
'Dim i As String
Application.ScreenUpdating = False

On Error Resume Next
Set Wb = Workbooks("MasterFile.xls")
If Wb Is Nothing Then '(MasterFile is not open so open it.)
Workbooks.Open Filename:="MasterFile.xls"
Set Wb = Nothing
On Error GoTo 0
Else '(MasterFile is already open so activate it.)
Windows("MasterFile").Activate
Set Wb = Nothing
On Error GoTo 0
End If

Windows("Test").Activate
Range("A1:K7").Select
Selection.Copy
Windows("MasterFile.xls").Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:="Archieve " & Format(Now(), "dd mm yyyy") & ".xls", _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWindow.Close
Range("A1").Select

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 

yankee428

Active Member
Joined
Apr 12, 2004
Messages
348
How about if you add a new sheet before pasting and instead of SaveAs, just use Save:
Code:
Sub CopyToMasterFile()
Dim Wb As Workbook
'Dim i As String
Application.ScreenUpdating = False

On Error Resume Next
Set Wb = Workbooks("MasterFile.xls")
If Wb Is Nothing Then '(MasterFile is not open so open it.)
Workbooks.Open Filename:="MasterFile.xls"
Set Wb = Nothing
On Error GoTo 0
Else '(MasterFile is already open so activate it.)
Windows("MasterFile").Activate
Set Wb = Nothing
On Error GoTo 0
End If

Windows("Test").Activate
Range("A1:K7").Select
Selection.Copy
Windows("MasterFile.xls").Activate

Sheets.Add 'added

Range("A1").Select
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
Application.DisplayAlerts = False
ActiveWorkbook.Save 'added
'ActiveWorkbook.SaveAs Filename:="Archieve " & Format(Now(), "dd mm yyyy") & ".xls", _
'FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
'ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWindow.Close
Range("A1").Select

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 

andrewrgd

Board Regular
Joined
Dec 30, 2004
Messages
78
Excellent - Thanks. Lastly how do you put an identifier in the sheet name of the masterfile i.e the name of the file your copying?

Thank you so much!
 

yankee428

Active Member
Joined
Apr 12, 2004
Messages
348
So if I read u right you are looking to change your sheet name based on some input somewhere. So you would need to adjust this to whatever your qualifier would be. Does that help?

Code:
Sub afdsf()
Dim qualifier As String
Dim msn As String
qualifier = "-1"
msn = Sheets(1).Name
Sheets(1).Name = msn & qualifier
End Sub
 

Forum statistics

Threads
1,147,622
Messages
5,742,205
Members
423,711
Latest member
luisfreitas

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
Top