VBA - Master Copy File not all columns will copy

DJosh27

New Member
Joined
Sep 25, 2017
Messages
1
I'm trying to create a macro that will take a daily pre-designed format sheet (people just input the numbers in to rows, and columns) and insert them into a combined yearly master. It will copy all the rows just fine it seems at the moment, but it stops at column CD and I need it to go to CQ. Thoughts? Also I'm not sure how to make it where it won't copy the same info each time. I'd like to make it where if it sees new data it just inputs it. I'm lost.

Sub TestingMacro()


Dim FolderPath As String, Filepath As String, Filename As String


FolderPath = "C:\Users\examplej\Desktop\Testing Macro"


Filepath = FolderPath & "*.xlsx*"


Filename = Dir(Filepath)


Dim lastrow As Long, lastcolumn As Long


Do While Filename <> ""
Workbooks.Open (FolderPath & Filename)


lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(4, 1), Cells(lastrow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close


erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
ActiveSheet.Paste Destination:=Worksheets("sheet1").Range(Cells(erow, 1), Cells(erow, lastcolumn))
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 1))
Filename = Dir


Loop
Application.DisplayAlerts = True
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
DJosh27,

Welcome to the Board.

If you want to hard code Column CQ as the lastcolumn...

Code:
lastcolumn = Columns("CQ").Column

If you want the lastcolumn to adjust to the real last column...

Code:
lastcolumn = ActiveSheet.Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column

Also I'm not sure how to make it where it won't copy the same info each time. I'd like to make it where if it sees new data it just inputs it.

One approach would be to use an IF statement in a loop to test for new data, for example...

Code:
Dim i as Long
For i = 4 To lastrow
    If Cells(i, 1).Value >= "9/24/2017" Then
        Range(Cells(i, 4), Cells(lastrow, lastcolumn)).Copy
        Exit For
    End If
Next i

Another approach would be to simply clear the master sheet before copying the data...

Code:
Rows("2:" & Cells(Rows.Count, "A").End(xlUp).Row).Clear

Cheers,

tonyyy
 
Last edited:
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