Trouble with syntax on VBA Paste Special

spitfire1956

New Member
Joined
Nov 3, 2009
Messages
6
I have been adapting some forum code to open all the files in a folder and copy cells C6 to C40, then use paste special to paste their values into a new column in a target workbook. I can get the code below to work - this does just a paste of the cells (highlighted blue), but I cannot get the syntax right to do a paste special of the values from C6 to C40. Anyone able to assist?

Sub Process_Files()
Dim sourceBook As Workbook, writeBook As Workbook
Dim sFil As String
Dim sPath As String
Dim writeCol As Long
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "T:\Users\APJ\Replacement Timesheet development"
.Show
If .SelectedItems.Count > 0 Then
sPath = .SelectedItems(1)
Else
MsgBox "Folder selection cancelled", vbInformation, Title:="Process Cancelled"
Exit Sub
End If
End With
ChDir sPath
sFil = Dir("*.xls")
Set writeBook = Workbooks.Add
writeCol = 1
Do While sFil <> ""
Set sourceBook = Workbooks.Open(sPath & "\" & sFil)
With writeBook.Sheets("Sheet1")
.Cells(1, writeCol) = sourceBook.Name ' write workbook name to row 1
sourceBook.Sheets("Time Recording").Range("C6:C40").Copy .Cells(2, writeCol) ' copy range to row 2
writeCol = writeCol + 1
End With
sourceBook.Close False
sFil = Dir
Loop
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
You can't do a paste special in a single line of code.

Rich (BB code):
Sub Process_Files()
Dim sourceBook As Workbook, writeBook As Workbook
Dim sFil As String
Dim sPath As String
Dim writeCol As Long
With Application.FileDialog(msoFileDialogFolderPicker)
    .InitialFileName = "T:\Users\APJ\Replacement Timesheet development"
    .Show
    If .SelectedItems.Count > 0 Then
        sPath = .SelectedItems(1)
    Else
        MsgBox "Folder selection cancelled", vbInformation, Title:="Process Cancelled"
        Exit Sub
    End If
End With
ChDir sPath
sFil = Dir("*.xls")
Set writeBook = Workbooks.Add
writeCol = 1
Do While sFil <> ""
Set sourceBook = Workbooks.Open(sPath & "\" & sFil)
With writeBook.Sheets("Sheet1")
    .Cells(1, writeCol) = sourceBook.Name ' write workbook name to row 1
    sourceBook.Sheets("Time Recording").Range("C6:C40").Copy 
    .Cells(2, writeCol).PasteSpecial xlPasteValues 'copy range to row 2
    writeCol = writeCol + 1
End With
sourceBook.Close False
sFil = Dir
Loop
End Sub
 
Upvote 0
You might be able to get one line if you reverse the syntax and use the .VALUE parameter:
Code:
.Range(.Cells(2, writeCol), .Cells(36, writeCol)).Value = sourceBook.Sheets("Time Recording").Range("C6:C40").Value
 
Upvote 0
Your suggested syntax has solved the problem - Thanks for the suggestion and taking the time to reply so promptly. Much appreciated.
 
Upvote 0

Forum statistics

Threads
1,215,493
Messages
6,125,128
Members
449,206
Latest member
burgsrus

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