Workbook1
是我所有数据所在的位置。
Workbook2
是我要复制数据的地方。
我想要一个按钮来执行以下操作:
-打开工作簿2
-从工作簿1复制数据单元(列出):
C3,S3,P3,J22,L22,J21,L21,C14,C15,C16,C17,C18,C19,C20,C21
-在workbook2中查找下一个空行
-将数据水平粘贴到workbook2中。因此,C3(来自workbook1)数据将粘贴到B4(来自workbook2)中,而S3将粘贴到C4中,依此类推。
这可能很费劲,但是是否可以让A列显示该特定行的数据传输日期?
任何建议表示赞赏!
让我知道您是否需要进一步的解释。
Private Sub CommandButton2_Click()
Dim wsMain As Worksheet
Dim wbData As Workbook
Dim wsData As Worksheet
Dim rngToCopy As Range
Dim C As Long
Dim ar As Range
Dim cl As Range
Dim LastRow As Long
Dim rngDestination As Range
Set wsMain = ThisWorkbook.ActiveSheet
Application.DisplayAlerts = False
' to open the workbook
Set wbData = Workbooks.Open("A:\folder\workbook2.xlsm", True)
Set wsData = wbData.Sheets("Completed")
Set rngToCopy = wsMain.Range("C3,S3,P3,J22,L22,J21,L21,C14,C15,C16,C17,C18,C19,C20,C21")
' to get the last row in the Completed worksheet
LastRow = wsData.Cells(wsData.Rows.Count, "B4").End(x1Up).Row
C = 1
For Each cl In rngToCopy
cl.Copy
wsData.Range("B" & C).PasteSpecial xlPasteValues
C = C + 1
Next cl
End Sub
答案 0 :(得分:1)
也许这行得通。似乎您的代码的最后一部分将值垂直粘贴,而不是水平粘贴。我进行了修改,使其水平粘贴了
Private Sub CommandButton2_Click()
Dim wsMain As Worksheet
Dim wbData As Workbook
Dim wsData As Worksheet
Dim rngToCopy As Range
Dim C As Long
Dim ar As Range
Dim cl As Range
Dim LastRow As Long
Dim rngDestination As Range
Set wsMain = ThisWorkbook.ActiveSheet
Application.DisplayAlerts = False
' to open the workbook
Set wbData = Workbooks.Open("A:\folder\workbook2.xlsm", True)
Set wsData = wbData.Sheets("Completed")
Set rngToCopy = wsMain.Range("C3,S3,P3,J22,L22,J21,L21,C14,C15,C16,C17,C18,C19,C20,C21")
' to get the last row in the Completed worksheet
LastRow = wsData.Cells(wsData.Rows.Count, "B").End(xlUp).Row 'get the last row number in Column B.
C = 1 'nth columns to go to right when codes iterate
For Each cl In rngToCopy
cl.Copy
wsData.Cells(LastRow + 1, 1 + C).PasteSpecial xlPasteValues ' Start from the last empty row (lastrow number + 1) and then start from column A (A = 1) then jump 1 step to the right in each iteration/loop.
C = C + 1 'Increase the column number by 1
wsData.Range("A" & LastRow + 1) = Now() ' Set date in column A for the row that was pasted
Next cl
End Sub
答案 1 :(得分:0)
我将为此更改粘贴代码:
Private Sub CommandButton2_Click()
Dim wsMain As Worksheet
Dim wbData As Workbook
Dim wsData As Worksheet
Dim rngToCopy As Range
Dim C As Long
Dim ar As Range
Dim cl As Range
Dim LastRow As Long
Dim rngDestination As Range
Set wsMain = ThisWorkbook.ActiveSheet
Application.DisplayAlerts = False
' to open the workbook
Set wbData = Workbooks.Open("A:\folder\workbook2.xlsm", True)
Set wsData = wbData.Sheets("Completed")
' to get the last row in the Completed worksheet
LastRow = wsData.Cells(wsData.Rows.Count, 2).End(xlUp).Row
' to paste the values
wsMain.Range("C3,S3,P3,J22,L22,J21,L21,C14,C15,C16,C17,C18,C19,C20,C21").Copy
wsData.Range("B" & LastRow).PasteSpecial xlPasteValues
'to get the Date and time when it was pasted
wsData.Range("A" & Lastrow) = Now()
End Sub