Copy Data from my current active workbook to a closed workbook

Martin sherk

Board Regular
Joined
Sep 11, 2022
Messages
94
Office Version
  1. 365
  2. 2016
Hello,

I need help copying data from my current active workbook(Code is in another workbook) to a closed file

My current active worksheet which I will copy data from has the name "Customers Data" sheet

My destination path for the closed workbook is C:\1.Martin\1.Work\Customers\October 2022
My destination workbook is: Customer Raw data.xlsx
My destination worksheet is: Customers Data

Can someone help construct a VBA to do the above, I will be thankful.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
@dmt32 you are right, sorry

Here we go

Rec_Export_with_Attlasachment_Status9_39.csv
ABCDEFGH
1Invoice no.CompanyPayeeCurrencyAmountIssue dateDue dateAvaliable
250411254USA1ENGD1USD1,002,558.0025.10.202230-Jun-22FALSE
350411254USA2ENGD2USD1,002,558.0025.10.202331-Jul-22FALSE
450411254USA3ENGD3USD1,002,558.0025.10.202430-Nov-22FALSE
550411254USA4ENGD4USD1,002,558.0025.10.202525-Sep-22FALSE
650411254USA5ENGD5USD1,002,558.0025.10.202625-May-22FALSE
750411254USA6ENGD6USD1,002,558.0025.10.202725-May-22FALSE
850411254USA7ENGD7USD1,002,558.0025.10.202825-May-22FALSE
950411254USA8ENGD8USD1,002,558.0025.10.202925-May-22FALSE
1050411254USA9ENGD9USD1,002,558.0025.10.203025-May-22FALSE
1150411254USA10ENGD10USD1,002,558.0025.10.203125-May-22FALSE
1250411254USA11ENGD11USD1,002,558.0025.10.203225-May-22FALSE
1350411254USA12ENGD12USD1,002,558.0025.10.203325-May-22FALSE
1450411254USA13ENGD13USD1,002,558.0025.10.203425-May-22FALSE
1550411254USA14ENGD14USD1,002,558.0025.10.203525-May-22FALSE
1650411254USA15ENGD15USD1,002,558.0025.10.203625-May-22FALSE
Sheet1
 
Upvote 0
Is the destination sheet identical & you want to paste the whole range to next blank row?

Dave
Book2
ABCDEFGH
1Invoice no.CompanyPayeeCurrency Amount Issue dateDue dateAvailable
2
3
4
5
6
7
8
9
10
11
12
13
14
Customer Main data


Hello Dave,

the destination sheet has a table as in the above screenshot, so I need to copy data from A2 to H2 after the header
also, I need to delete any data in the destination sheet except for the header before copying the new data to the destination sheet
 
Upvote 0
Book2
ABCDEFGH
1Invoice no.CompanyPayeeCurrency Amount Issue dateDue dateAvailable
2
3
4
5
6
7
8
9
10
11
12
13
14
Customer Main data


Hello Dave,

the destination sheet has a table as in the above screenshot, so I need to copy data from my sheet to the destination sheet in A2 to H2 after the header
also, I need to delete any data in the destination sheet except for the header before copying the new data to the destination sheet
 
Upvote 0
Hi,
only lightly tested but see if this code will do what you want

Place in a STANDARD module

VBA Code:
Sub SubmitData()
    Dim FullName            As String
    Dim wsCustomersData     As Worksheet
    Dim wbCustomerRawData   As Workbook
    Dim CopyFromRange       As Range, CopyToRange As Range
    
    Const FilePath As String = "C:\1.Martin\1.Work\Customers\October 2022\"
    Const FileName  As String = "Customer Raw data.xlsx"
    
    FullName = FilePath & FileName
    
    Set wsCustomersData = ThisWorkbook.Worksheets("Customers Data")
    'size the copy range (sheet must not be protected)
    Set CopyFromRange = wsCustomersData.Range("A1").CurrentRegion
    
    On Error GoTo myerror
    If Not Dir(FullName, vbDirectory) = vbNullString Then
        Application.ScreenUpdating = False
        Set wbCustomerRawData = Workbooks.Open(FullName, 0, False)
        'size to destination range
        Set CopyToRange = _
        wbCustomerRawData.Sheets(1).Cells(1, 1).Resize(, CopyFromRange.Columns.Count)
        
        'ensure header values match
        CopyToRange.Value = CopyFromRange.Rows(1).Value
        
        'copy data to sheet
        CopyFromRange.AdvancedFilter Action:=xlFilterCopy, _
        CopyToRange:=CopyToRange
        
    Else
        'file not found
        Err.Raise 53
    End If
    
myerror:
    If Not wbCustomerRawData Is Nothing Then wbCustomerRawData.Close CBool(Err = 0)
    Application.ScreenUpdating = True
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
    
End Sub

I have assumed that the destination worksheet in the closed file is the first (or only) sheet in the workbook.

Dave
 
Upvote 0
Hi,
only lightly tested but see if this code will do what you want

Place in a STANDARD module

VBA Code:
Sub SubmitData()
    Dim FullName            As String
    Dim wsCustomersData     As Worksheet
    Dim wbCustomerRawData   As Workbook
    Dim CopyFromRange       As Range, CopyToRange As Range
   
    Const FilePath As String = "C:\1.Martin\1.Work\Customers\October 2022\"
    Const FileName  As String = "Customer Raw data.xlsx"
   
    FullName = FilePath & FileName
   
    Set wsCustomersData = ThisWorkbook.Worksheets("Customers Data")
    'size the copy range (sheet must not be protected)
    Set CopyFromRange = wsCustomersData.Range("A1").CurrentRegion
   
    On Error GoTo myerror
    If Not Dir(FullName, vbDirectory) = vbNullString Then
        Application.ScreenUpdating = False
        Set wbCustomerRawData = Workbooks.Open(FullName, 0, False)
        'size to destination range
        Set CopyToRange = _
        wbCustomerRawData.Sheets(1).Cells(1, 1).Resize(, CopyFromRange.Columns.Count)
       
        'ensure header values match
        CopyToRange.Value = CopyFromRange.Rows(1).Value
       
        'copy data to sheet
        CopyFromRange.AdvancedFilter Action:=xlFilterCopy, _
        CopyToRange:=CopyToRange
       
    Else
        'file not found
        Err.Raise 53
    End If
   
myerror:
    If Not wbCustomerRawData Is Nothing Then wbCustomerRawData.Close CBool(Err = 0)
    Application.ScreenUpdating = True
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
   
End Sub

I have assumed that the destination worksheet in the closed file is the first (or only) sheet in the workbook.

Dave
Appreciate the time taken preparing this, Thanks, Dave.
please note that I have 3 sheets in the destination sheet, so i need a way where i can specify which sheet to copy data to.
 
Upvote 0
Appreciate the time taken preparing this, Thanks, Dave.
please note that I have 3 sheets in the destination sheet, so i need a way where i can specify which sheet to copy data to.

either make the destination sheet the first sheet in the workbook or to specify a name, change this line

VBA Code:
Set CopyToRange = _
        wbCustomerRawData.Sheets(1).Cells(1, 1).Resize(, CopyFromRange.Columns.Count)

to this

Rich (BB code):
Set CopyToRange = _
        wbCustomerRawData.WorkSheets("Customer Main Data").Cells(1, 1).Resize(, CopyFromRange.Columns.Count)

Change the sheet name in RED as required

Dave
 
Upvote 0
either make the destination sheet the first sheet in the workbook or to specify a name, change this line

VBA Code:
Set CopyToRange = _
        wbCustomerRawData.Sheets(1).Cells(1, 1).Resize(, CopyFromRange.Columns.Count)

to this

Rich (BB code):
Set CopyToRange = _
        wbCustomerRawData.WorkSheets("Customer Main Data").Cells(1, 1).Resize(, CopyFromRange.Columns.Count)

Change the sheet name in RED as required

Dave
It worked Flawlessly !! time saved thanks to you.

one last request please, it copies data to the destination file but after row 1000, the data copied is without table format, can i fix that?
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,370
Members
449,080
Latest member
Armadillos

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