I would like to copy/paste some columns (always the same) from one Workbook to another. I can't make my code to do this between workbooks. And one more small thing I want paste values to the 4th row in target sheet and all I could do by myself is to paste it into second one :/
Something like this code but this one works only pasting values from one sheet to another in the same xls file:
EDIT: code with my attempt of copy/paste between xls files. What do I do wrong, because it doesn't work.
Sub Paste_columns()
Dim x As Worksheet, r As Long, y As Worksheet
Set x = Workbooks("Bench.xlsm").Worksheets("Test-Sheet")
Set y = Workbooks("Pres.xls").Worksheets("Paste_tab")
With y
For r = 2 To y.Range("B" & Rows.Count).End(xlUp).Row
If y.Cells(r, 2).Value > 0 Then
x.Range("B" & Rows.Count).End(xlUp)(2).Value = .Cells(r, 2)
x.Range("C" & Rows.Count).End(xlUp)(2).Value = .Cells(r, 3)
x.Range("D" & Rows.Count).End(xlUp)(2).Value = .Cells(r, 4)
x.Range("E" & Rows.Count).End(xlUp)(2).Value = .Cells(r, 5)
x.Range("H" & Rows.Count).End(xlUp)(2).Value = .Cells(r, 8)
x.Range("I" & Rows.Count).End(xlUp)(2).Value = .Cells(r, 9)
x.Range("M" & Rows.Count).End(xlUp)(2).Value = .Cells(r, 13)
x.Range("O" & Rows.Count).End(xlUp)(2).Value = .Cells(r, 15)
x.Range("Q" & Rows.Count).End(xlUp)(2).Value = .Cells(r, 17)
x.Range("S" & Rows.Count).End(xlUp)(2).Value = .Cells(r, 19)
x.Range("V" & Rows.Count).End(xlUp)(2).Value = .Cells(r, 22)
x.Range("W" & Rows.Count).End(xlUp)(2).Value = .Cells(r, 23)
Else: End
End If
Next r
End With
End Sub
Finally I've did something like this and it works properly. Thanks to the @Harassed Dad - I've used some of your code. I think it could be more universal but for me it's OK now.
Sub PasteToTemplate()
Dim xD As Workbook
Dim xS As Workbook
Dim wsSource As Worksheet
Dim x As Worksheet
Dim tempN As Worksheet
Dim tN As String
Dim sN As String
With Sheets("Source_sample_size")
sN = .Range("PresName").Value
End With
Set xS = Workbooks(sN)
Set wsSource = xS.Worksheets("Paste_tab")
Set tempN = xS.Worksheets("Source_sample_size")
With tempN
tN = .Range("tempName").Value
End With
Set xD = Workbooks.Open(tN)
Set x = xD.Worksheets("Test-Sheet")
wsSource.Range("A2:F" & wsSource.Range("A" & wsSource.Rows.Count).End(xlUp).Row).Copy
x.Range("B4").PasteSpecial (xlPasteValues)
wsSource.Range("H2:I" & wsSource.Range("A" & wsSource.Rows.Count).End(xlUp).Row).Copy
x.Range("H4").PasteSpecial (xlPasteValues)
wsSource.Range("J2:J" & wsSource.Range("A" & wsSource.Rows.Count).End(xlUp).Row).Copy
x.Range("M4").PasteSpecial (xlPasteValues)
wsSource.Range("K2:K" & wsSource.Range("A" & wsSource.Rows.Count).End(xlUp).Row).Copy
x.Range("O4").PasteSpecial (xlPasteValues)
wsSource.Range("L2:L" & wsSource.Range("A" & wsSource.Rows.Count).End(xlUp).Row).Copy
x.Range("AI4").PasteSpecial (xlPasteValues)
wsSource.Range("M2:M" & wsSource.Range("A" & wsSource.Rows.Count).End(xlUp).Row).Copy
x.Range("AK4").PasteSpecial (xlPasteValues)
wsSource.Range("P2:P" & wsSource.Range("A" & wsSource.Rows.Count).End(xlUp).Row).Copy
x.Range("AM4").PasteSpecial (xlPasteValues)
End Sub