VBA Copy Data to Another Workbook

Redwolfx

Well-known Member
Joined
Feb 22, 2013
Messages
1,161
Good Afternoon,
So far I have the following Code

Code:
Sub Export()
Dim myCell8 As Range
Dim LastRow As Long
Dim nextRow As Long
Sheets("Original Refund").Select
LastRow = Range("A2000").End(xlUp).Row
For Each myCell8 In Worksheets("Original Refund").Range(Cells(9, 1), Cells(LastRow, 1))
With Worksheets("Data")
 nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
.Cells(nextRow, 1).Value = ActiveWorkbook.Name
.Cells(nextRow, 2).Value = Range("F6").Value
.Cells(nextRow, 3).Value = Range("D4").Value
.Cells(nextRow, 4).Value = Application.UserName
.Cells(nextRow, 5).Value = Range("B5").Value
.Cells(nextRow, 6).Value = myCell8.Value
.Cells(nextRow, 7).Value = Range("D5").Value
.Cells(nextRow, 8).Value = myCell8(1, 2).Value
.Cells(nextRow, 9).Value = Range("D6").Value
.Cells(nextRow, 10).Value = Range("B4").Value
End With
Next myCell8
End Sub

This works great for moving data from one sheet to another within the same workbook. What I'm trying to do is have the data moved to another workbook. I know that I can take this, then add code to copy and paste the new sheet to another workbook, but I'm trying to skip having to have a sheet available just to house data to move to another sheet.

What needs to be Added/Changed to acomplish this?

Thanks.

-Red
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
What you need to do is to directly address the other workbook once opened (or use ADO if closed). So if you have the following defines, it would look something like this:
Code:
Dim wkBook1 As Workbook
Dim wkBook2 As Workbook
Dim fromRange As Range
Dim toRange As Range

wkBook1.fromRange().copy destination:=wkBook2.Range()

Obviously, there would be a sheet in there, etc...
 
Upvote 0
The problem is that without moving it to another page, it's not a perfect Copy, As you can see from my code, information is puled from various cells and organized through the VBA. I'm tring to accomplish the same thing, but across workbooks.
 
Upvote 0
Using your code and just addressing the workbook, it would look something like this (untested):
Code:
Sub Export()
    Dim wkBook1 As Workbook
    Dim wkBook2 As Workbook
    Dim myCell8 As Range
    Dim LastRow As Long
    Dim nextRow As Long
    
    LastRow = wkBook1.Sheets("Original Refund").Range("A2000").End(xlUp).Row
    With wkBook1.Sheets("Original Refund").Range("A1:A" & LastRow)
        For Each myCell8 In .Range(Cells(9, 1), Cells(LastRow, 1))
            With wkBook2.Worksheets("Data")
                 nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
                .Cells(nextRow, 1).Value = ActiveWorkbook.Name
                .Cells(nextRow, 2).Value = wkBook1.Sheets("Original Refund").Range("F6").Value
                .Cells(nextRow, 3).Value = wkBook1.Sheets("Original Refund").Range("D4").Value
                .Cells(nextRow, 4).Value = Application.UserName
                .Cells(nextRow, 5).Value = wkBook1.Sheets("Original Refund").Range("B5").Value
                .Cells(nextRow, 6).Value = myCell8.Value
                .Cells(nextRow, 7).Value = wkBook1.Sheets("Original Refund").Range("D5").Value
                .Cells(nextRow, 8).Value = myCell8(1, 2).Value
                .Cells(nextRow, 9).Value = wkBook1.Sheets("Original Refund").Range("D6").Value
                .Cells(nextRow, 10).Value = wkBook1.Sheets("Original Refund").Range("B4").Value
            End With
        Next myCell8
    End WIth
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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