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.
 

Some videos you may like

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,185
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
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,792
Messages
5,513,466
Members
408,953
Latest member
Skiig

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top