VBA - Copy and Paste varying rows in Closed Workbook

ABF

New Member
Joined
Oct 1, 2020
Messages
7
Office Version
  1. 2010
Platform
  1. Windows
I have a file that I pull weekly (Site x) and I need to copy the data from this sheet to the bottom of the other file called "Site x Compiled". I have the code below, but the part of copy and paste is not working.
I'm still learning how VBA works, so can someone help me accomplish this?

````
Sub Compiling()

Dim InputFile As Workbook
Dim OutputFile As Workbook
Dim Inputpath As String
Dim Outputpath As String


'OKAY Set path for Input & Output
fileInputpath = "C:\Users\Desktop\Workbooks\"
Outputpath = "C:\Users\Desktop\Workbooks\"

'OKAY Open both workbooks first:
Set InputFile = Workbooks.Open(Inputpath & "C:\Users\Desktop\Workbooks\Site x.xlsx")
Set OutputFile = Workbooks.Open(Outputpath & "Site x Compiled.xlsx")

'NOT RUNNING PROPERLY - Now, copy what you want from InputFile:
InputFile.Sheets("Sheet1").Activate
InputFile.Sheets("Sheet1").Range("A2", Range("A2").End(xlDown).End(xlToRight)).copy

'NOT RUNNING PROPERLY - Now, paste to OutputFile worksheet:
OutputFile.Sheets("Sheet1").Activate
OutputFile.Sheets("Sheet1").Range("A2", Range("A2").End(xlDown)).Offset(1).PasteSpecial Paste:=xlPasteFormats

'OKAY Close InputFile & OutputFile:
Application.DisplayAlerts = False
InputFile.Close
OutputFile.Close savechanges:=True


End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try:
VBA Code:
Sub Compiling()
    Application.ScreenUpdating = False
    Dim InputFile As Workbook, OutputFile As Workbook, Inputpath As String, Outputpath As String
    fileInputpath = "C:\Users\Desktop\Workbooks\"
    Outputpath = "C:\Users\Desktop\Workbooks\"
    Set InputFile = Workbooks.Open(Inputpath & "Site x.xlsx")
    Set OutputFile = Workbooks.Open(Outputpath & "Site x Compiled.xlsx")
    With InputFile.Sheets("Sheet1")
        .Range("A2", .Range("A2").End(xlDown).End(xlToRight)).Copy
        OutputFile.Sheets("Sheet1").Range("A2", OutputFile.Sheets("Sheet1").Range("A2").End(xlDown)).Offset(1).PasteSpecial Paste:=xlPasteFormats
    End With
    Application.DisplayAlerts = False
    InputFile.Close False
    OutputFile.Close True
    Application.ScreenUpdating = True
End Sub
 
  • Like
Reactions: ABF
Upvote 0
Try:
VBA Code:
Sub Compiling()
    Application.ScreenUpdating = False
    Dim InputFile As Workbook, OutputFile As Workbook, Inputpath As String, Outputpath As String
    fileInputpath = "C:\Users\Desktop\Workbooks\"
    Outputpath = "C:\Users\Desktop\Workbooks\"
    Set InputFile = Workbooks.Open(Inputpath & "Site x.xlsx")
    Set OutputFile = Workbooks.Open(Outputpath & "Site x Compiled.xlsx")
    With InputFile.Sheets("Sheet1")
        .Range("A2", .Range("A2").End(xlDown).End(xlToRight)).Copy
        OutputFile.Sheets("Sheet1").Range("A2", OutputFile.Sheets("Sheet1").Range("A2").End(xlDown)).Offset(1).PasteSpecial Paste:=xlPasteFormats
    End With
    Application.DisplayAlerts = False
    InputFile.Close False
    OutputFile.Close True
    Application.ScreenUpdating = True
End Sub
it still not pasting the data.... :(
 
Upvote 0
You used
VBA Code:
Paste:=xlPasteFormats
This paste the formats only. Do you want to paste formats and cell content?
 
  • Like
Reactions: ABF
Upvote 0
You used
VBA Code:
Paste:=xlPasteFormats
This paste the formats only. Do you want to paste formats and cell content?
I got it, i changed to xlPasteValues now, but the data is pasting over the old data, i would like to paste at the bottom
 
Upvote 0
Try:
VBA Code:
Sub Compiling()
    Application.ScreenUpdating = False
    Dim InputFile As Workbook, OutputFile As Workbook, Inputpath As String, Outputpath As String
    fileInputpath = "C:\Users\Desktop\Workbooks\"
    Outputpath = "C:\Users\Desktop\Workbooks\"
    Set InputFile = Workbooks.Open(Inputpath & "Site x.xlsx")
    Set OutputFile = Workbooks.Open(Outputpath & "Site x Compiled.xlsx")
    With InputFile.Sheets("Sheet1")
        .Range("A2", .Range("A2").End(xlDown).End(xlToRight)).Copy OutputFile.Sheets("Sheet1").Cells(OutputFile.Sheets("Sheet1").Rows.Count, "A").End(xlUp).Offset(1)
    End With
    Application.DisplayAlerts = False
    InputFile.Close False
    OutputFile.Close True
    Application.ScreenUpdating = True
End Sub
 
  • Like
Reactions: ABF
Upvote 0
Try:
VBA Code:
Sub Compiling()
    Application.ScreenUpdating = False
    Dim InputFile As Workbook, OutputFile As Workbook, Inputpath As String, Outputpath As String
    fileInputpath = "C:\Users\Desktop\Workbooks\"
    Outputpath = "C:\Users\Desktop\Workbooks\"
    Set InputFile = Workbooks.Open(Inputpath & "Site x.xlsx")
    Set OutputFile = Workbooks.Open(Outputpath & "Site x Compiled.xlsx")
    With InputFile.Sheets("Sheet1")
        .Range("A2", .Range("A2").End(xlDown).End(xlToRight)).Copy OutputFile.Sheets("Sheet1").Cells(OutputFile.Sheets("Sheet1").Rows.Count, "A").End(xlUp).Offset(1)
    End With
    Application.DisplayAlerts = False
    InputFile.Close False
    OutputFile.Close True
    Application.ScreenUpdating = True
End Sub
thank you so much for your help, you are almost there! the only thing it that is pasting only from column A to H and the data I'm copy is from column A to U, maybe is not working because there are some empty columns? but idk how to resolve...
 
Upvote 0
Try:
VBA Code:
Sub Compiling()
    Application.ScreenUpdating = False
    Dim InputFile As Workbook, OutputFile As Workbook, Inputpath As String, Outputpath As String, lRow As Long
    fileInputpath = "C:\Users\Desktop\Workbooks\"
    Outputpath = "C:\Users\Desktop\Workbooks\"
    Set InputFile = Workbooks.Open(Inputpath & "Site x.xlsx")
    Set OutputFile = Workbooks.Open(Outputpath & "Site x Compiled.xlsx")
    With InputFile.Sheets("Sheet1")
        lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        .Range("A2:U" & lRow).Copy OutputFile.Sheets("Sheet1").Cells(OutputFile.Sheets("Sheet1").Rows.Count, "A").End(xlUp).Offset(1)
    End With
    Application.DisplayAlerts = False
    InputFile.Close False
    OutputFile.Close True
    Application.ScreenUpdating = True
End Sub
 
  • Like
Reactions: ABF
Upvote 0
Try:
VBA Code:
Sub Compiling()
    Application.ScreenUpdating = False
    Dim InputFile As Workbook, OutputFile As Workbook, Inputpath As String, Outputpath As String, lRow As Long
    fileInputpath = "C:\Users\Desktop\Workbooks\"
    Outputpath = "C:\Users\Desktop\Workbooks\"
    Set InputFile = Workbooks.Open(Inputpath & "Site x.xlsx")
    Set OutputFile = Workbooks.Open(Outputpath & "Site x Compiled.xlsx")
    With InputFile.Sheets("Sheet1")
        lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        .Range("A2:U" & lRow).Copy OutputFile.Sheets("Sheet1").Cells(OutputFile.Sheets("Sheet1").Rows.Count, "A").End(xlUp).Offset(1)
    End With
    Application.DisplayAlerts = False
    InputFile.Close False
    OutputFile.Close True
    Application.ScreenUpdating = True
End Sub
OMG you are THE MAN!!!! THANK YOU SOOOOO MUCH!!!
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,215
Members
452,618
Latest member
Tam84

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