Set Source and Destination in VBA

posfog

Board Regular
Joined
Jun 2, 2009
Messages
171
Hi All

I have a macro that allows me to move information from one sheet to another in the same workbook, but i require it to move to a different work book using the following bit of code

' Set source and destination sheets
Set src = Sheets("TOUCH SCREEN")
Set dst = Sheets("RAW DATA")

How would i need to change this if i want the destination to be a different workbook and sheet name?


Thanks in advance

P
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Not that i'm any good with this stuff a quick google gives me the below result:

Sub Open_Workbook()

Dim nb As Workbook, tw As Workbook, ts As Worksheet
a = Application.GetOpenFilename
If a = False Or IsEmpty(a) Then Exit Sub
With Application
.ScreenUpdating = False
End With
Set tw = ThisWorkbook
Set ts = tw.ActiveSheet
Set nb = Workbooks.Open(a)
nb.Sheets("Sheet1").Copy After:=Workbooks("file name.xlsm").Sheets("Pivot Table")

End Sub

Whether you need all of the above information i doubt it but the relevant information you will need is to copy "sheet1" into the worksheet of "name" with the sheet name being "name"
Hopefully this helps you a little more?
If you needed a more dynamic approach with name changing files then i'm also stuck.
 
Upvote 0
Assuming the source workbook is open...

Change the workbook name accordingly

Code:
Set src = Workbooks("WORKBOOKNAME").Worksheets("TOUCHSCREEN")

If it isn't open you will need to open it first either manually or in code
 
Last edited:
Upvote 0
Assuming the source workbook is open...

Change the workbook name accordingly

Code:
Set src = Workbooks("WORKBOOKNAME").Worksheets("TOUCHSCREEN")

If it isn't open you will need to open it first either manually or in code

What if the work book in question is closed, would it still send the details to it and would it save the file?


I would put the whole VBA code in here but its very lengthy
 
Upvote 0
No. You'd have to open the file with something like:
Code:
    Dim srcWB As Workbook
    Set srcWB = Workbooks.Open("Yourpath/Yourworkbookname")
    
    Set src = srcWB.Worksheets("TOUCHSCREEN")

There is a lot of code out there that shows you more proficient methods that let the user select the file etc...
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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