Need VBA to Open a different Excel workbook and copy and paste data into a workbook

jswinney1

New Member
Joined
Jul 19, 2010
Messages
47
I need code that will open a different excel workbook off of my drive and copy and paste data from it into a workbook that I am working in and then close the data workbook.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
I'm sure you do!

You'll have to be a lot more specific if you expect any help.
 

jswinney1

New Member
Joined
Jul 19, 2010
Messages
47
I would like to open a new workbook on my hard drive called London. With in the London Workbook, I want to copy Cells B1:AG25. I want to activate my workbook that is already open called FX and paste the data from london into my London_FX tab starting with cell A6. Is this better?
 

rconverse

Well-known Member
Joined
Nov 29, 2007
Messages
1,187
This should get you started. You'll have to change the bolded to suit what your sheet numbers, workbook names, and file paths.

Rich (BB code):
Sub macCOPY()
Dim strPath As String
Dim strName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim wkb As Workbook
Dim wks As Worksheet

'turn off events
Call ToggleEvents(False)

'set variables for filepath and file name
strPath = "F:\OpenAccess\Daily_Sales\" '<-- Change to suit
strName = "Orders_Entered_" & Format(Date, "mmddyyyy") & ".xls"  '<-- Change to suit

'open the file for that day.
Call OpenWb(strPath, strName)

'set variables for the workbook and worksheet
Set wb = Workbooks("Orders_Entered_" & Format(Date, "mmddyyyy") & ".xls") '<-- Change to suit
Set ws = wb.Sheets(1)  '<-- Change to suit

strPath = "F:\OpenAccess\Daily_Sales\"  '<-- Change to suit
strName = "OrderEntryTemplate.xlsx"  '<-- Change to suit

'Open the table with the current data and copy it into the template
Call OpenWb(strPath, strName)

'set variables for the workbook and worksheet
Set wkb = Workbooks("OrderEntryTemplate.xlsx")  '<-- Change to suit
Set wks = wkb.Sheets(2)  '<-- Change to suit

'copy cells from the daily excel spreadsheet created
'wb.ws.Cells.Copy
wb.Sheets(1).Cells.Copy

'paste data in the daily template
wkb.Sheets(2).Range("A1").PasteSpecial xlPasteValues  '<--Change sheet number to suit

'save the workbook with today's date.
strPath = "F:\OpenAccess\Daily_Sales\"  '<--Change to suit
strName = "OrdersEntered_" & Format(Date, "mmddyyyy") & ".xls"  '<--Change to suit

wkb.SaveAs strPath & strName, xlNormal

'close the workbooks
wkb.Close
wb.Close

Call ToggleEvents(True)

End Sub

Sub ToggleEvents(blnState As Boolean)

'Originally written by firefytr
    
    With Excel.Application
        .DisplayAlerts = blnState
        .EnableEvents = blnState
        .ScreenUpdating = blnState
        If blnState Then .CutCopyMode = False
        If blnState Then .StatusBar = False
    End With
    
End Sub
Sub OpenWb(strPath, strName)

'sub created 5/1/2009
'created by Roger Converse

'Check to see if the workbook to be modified is open.  If so, create an error message.  If not, open the file.

If WbOpen(strPath & strName) = True Then
    
    'error message
    MsgBox "This workbook is already open.  That should not be the case.  If this is a rerun" _
    & " then you will need to delete the previous output prior to rerunning.", vbCritical
    
    'turn on events and exit
    Call ToggleEvents(True)
    
    Exit Sub
    
Else
    
    'open the file
    Set wb = Workbooks.Open(strPath & strName)
    
End If

End Sub

HTH,
Roger
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,613
Messages
5,832,707
Members
430,155
Latest member
spacedad41

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