Please help with code

lakke2120

New Member
Joined
Aug 21, 2014
Messages
32
Hello,
I am new to coding and I am having a problem with this code. I would like it to copy and paste special values and not formulas. Everything else is working fine.
thank you

Sub CombineData()
Dim myDir As String, filename As String
Dim lrdata As Long
Dim masterws As Worksheet

Application.ScreenUpdating = False
Set masterws = ThisWorkbook.Sheets("Data")
myDir = "E:\test folder" '<- Change here (Folder path)
filename = Dir(myDir & "\Payroll*.xls*")
Do While filename <> ""
With Workbooks.Open(myDir & "\" & filename)
With .Sheets("totals")
lrdata = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A1:T625" & lrdata).Copy _
Destination:=masterws.Range("A" & masterws.Rows.Count).End(xlUp).Offset(1)
End With
.Close False
End With
filename = Dir
Loop
Set masterws = Nothing
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
You have to split it up into two separate operations.

Code:
      .Range("A1:T625" & lrdata).Copy
      masterws.Range("A" & masterws.Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues

Post back if this is not clear.
 
Upvote 0
Try
Rich (BB code):
With Sheets("totals")
lrdata = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A1:T625" & lrdata).Copy
masterws.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
End With
 
Upvote 0
Thank you SO MUCH for answering so quick. That was really cool. I am getting a "loop without do" error and not sure how to fix it.
thanks again
 
Upvote 0
Re-post the code as you have changed it.
 
Upvote 0
Here is is:
Sub CombineData()
Dim myDir As String, filename As String
Dim lrdata As Long
Dim masterws As Worksheet

Application.ScreenUpdating = False
Set masterws = ThisWorkbook.Sheets("Data")
myDir = "E:\test folder2" '<- Change here (Folder path)
filename = Dir(myDir & "\Payroll*.xls*")
Do While filename <> ""
With Workbooks.Open(myDir & "\" & filename)
With Sheets("totals")
lrdata = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A1:T625" & lrdata).Copy
masterws.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
End With
filename = Dir
Loop
Set masterws = Nothing
Application.ScreenUpdating = True

End Sub
 
Upvote 0
The error message is pretty accurate - you're missing an End to a With:)

Rich (BB code):
Do While filename <> ""
  With Workbooks.Open(myDir & "\" & filename)
    With Sheets("totals")
      lrdata = .Range("A" & .Rows.Count).End(xlUp).Row
      .Range("A1:T625" & lrdata).Copy
      masterws.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
    End With
    filename = Dir
  End With
Loop
 
Upvote 0
The error message is pretty accurate - you're missing an End to a With:)

Rich (BB code):
Do While filename <> ""
  With Workbooks.Open(myDir & "\" & filename)
    With Sheets("totals")
      lrdata = .Range("A" & .Rows.Count).End(xlUp).Row
      .Range("A1:T625" & lrdata).Copy
      masterws.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
    End With
    filename = Dir
  End With
Loop

When I get a Next without For error I also look for a missing End If. Isn't excel helpful :).
 
Upvote 0

Forum statistics

Threads
1,213,532
Messages
6,114,176
Members
448,554
Latest member
Gleisner2

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