Forum Discussion
VBA copy data from another workbook (specific range) into a different workbook to a specific Sheet.
To accomplish this task using VBA, you will need to write a macro that loops through each customer, opens their respective order file, copies the data from the specified range, and then pastes it into the corresponding sheet in the invoice file.
Here is a basic outline of the steps you will need to follow:
- Loop through each customer.
- Open the order file for the current customer.
- Copy the data from columns A to AE in the order file.
- Open the invoice file for the current customer.
- Paste the copied data into the appropriate sheet in the invoice file.
Here is a sample VBA code that demonstrates how to achieve this:
Vba code is untested, please backup your file first.
Sub CopyDataToInvoice()
Dim customerList As Variant
Dim customer As Variant
Dim orderFilePath As String
Dim invoiceFilePath As String
Dim orderWorkbook As Workbook
Dim invoiceWorkbook As Workbook
' Define the list of customers
customerList = Array("CustomerA", "CustomerB", "CustomerC") ' Add all your customers here
' Loop through each customer
For Each customer In customerList
' Define the file paths for order and invoice files
orderFilePath = "Path\to\Order\File\" & customer & "_Orders.xlsx"
invoiceFilePath = "Path\to\Invoice\File\" & customer & "_Invoice.xlsx"
' Open the order file
Set orderWorkbook = Workbooks.Open(orderFilePath)
' Copy data from columns A to AE in the order file
orderWorkbook.Sheets("SheetName").Range("A:AE").Copy
' Close the order file
orderWorkbook.Close False
' Open the invoice file
Set invoiceWorkbook = Workbooks.Open(invoiceFilePath)
' Paste the copied data into the appropriate sheet in the invoice file
invoiceWorkbook.Sheets("OrderData").Range("A1").PasteSpecial Paste:=xlPasteValues
' Close the invoice file
invoiceWorkbook.Close True
Next customer
End SubMake sure to replace "Path\to\Order\File\", "Path\to\Invoice\File\", "CustomerA", "CustomerB", "CustomerC", "SheetName", and "OrderData" with the actual paths, customer names, and sheet names in your files.
Also, ensure that you save the Excel files as macro-enabled (.xlsm) files if they contain VBA code. The text, steps and code were created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.