如何 copy/paste 从一个 xls 到另一个 xls 的选定列
How to copy/paste selected columns from one xls to another
我想 copy/paste 从一个工作簿到另一个工作簿的一些列(总是相同的)。我无法让我的代码在工作簿之间执行此操作。
还有一件小事,我想将值粘贴到目标 sheet 的第 4 行,而我自己所能做的就是将其粘贴到第二行:/
类似于这段代码,但这段代码只能将值从一个 sheet 粘贴到同一 xls 文件中的另一个:
编辑:我在 xls 文件之间尝试 copy/paste 的代码。我做错了什么,因为它不起作用。
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
使用有意义的变量名——尤其是在你学习的时候:
Sub Example()
Dim wbSource As Workbook
Dim wbDestination As Workbook
Dim wsSource As Worksheet
Dim wsDestination As Worksheet
Dim rSource As Range
Dim rDestination As Range
Set wbSource = Workbooks("Pres.xls")
Set wsSource = wbSource.Worksheets("Paste_tab")
Set wbDestination = Workbooks("Bench.xlsm")
Set wsDestination = wbDestination.Worksheets("Test-Sheet")
Set rSource = wsSource.Range("b2:b" & wsSource.Range("b" & wsSource.Rows.Count).End(xlUp).Row)
For Each rDestination In wsDestination.Range("B4:W4")
If InStr("BCDEHIMOQSVW", Left(rDestination.AddressLocal(False, False), 1)) > 0 Then
' rSource.Copy rDestination
'=============if only values wanted ===========
rsource.copy
rdestination.pastespecial xlpastevalues
'==================
End If
Next rDestination
End Sub
最后我做了这样的事情并且它工作正常。感谢@Harassed Dad - 我使用了您的一些代码。我认为它可能更通用,但对我来说现在还可以。
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
我想 copy/paste 从一个工作簿到另一个工作簿的一些列(总是相同的)。我无法让我的代码在工作簿之间执行此操作。 还有一件小事,我想将值粘贴到目标 sheet 的第 4 行,而我自己所能做的就是将其粘贴到第二行:/
类似于这段代码,但这段代码只能将值从一个 sheet 粘贴到同一 xls 文件中的另一个:
编辑:我在 xls 文件之间尝试 copy/paste 的代码。我做错了什么,因为它不起作用。
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
使用有意义的变量名——尤其是在你学习的时候:
Sub Example()
Dim wbSource As Workbook
Dim wbDestination As Workbook
Dim wsSource As Worksheet
Dim wsDestination As Worksheet
Dim rSource As Range
Dim rDestination As Range
Set wbSource = Workbooks("Pres.xls")
Set wsSource = wbSource.Worksheets("Paste_tab")
Set wbDestination = Workbooks("Bench.xlsm")
Set wsDestination = wbDestination.Worksheets("Test-Sheet")
Set rSource = wsSource.Range("b2:b" & wsSource.Range("b" & wsSource.Rows.Count).End(xlUp).Row)
For Each rDestination In wsDestination.Range("B4:W4")
If InStr("BCDEHIMOQSVW", Left(rDestination.AddressLocal(False, False), 1)) > 0 Then
' rSource.Copy rDestination
'=============if only values wanted ===========
rsource.copy
rdestination.pastespecial xlpastevalues
'==================
End If
Next rDestination
End Sub
最后我做了这样的事情并且它工作正常。感谢@Harassed Dad - 我使用了您的一些代码。我认为它可能更通用,但对我来说现在还可以。
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