VBA 代码将值复制并粘贴到另一张纸中并移动到下一行

问题描述 投票:0回答:1

我需要创建一个按钮,单击它会将(值)日期从sheet1复制并粘贴到sheet2,如果我再次单击它,将转到下一行,依此类推enter image description here

我发现了不同的VBA代码,但它只能工作一次,并且不能进入下一行

excel vba coding-style copy-paste vba6
1个回答
0
投票

添加按钮并分配此代码:

Sub CopyData()

'Define the source and destination worksheets
Dim wsSource As Worksheet
Dim wsDestination As Worksheet

Set wsSource = ThisWorkbook.Sheets("Sheet1")
Set wsDestination = ThisWorkbook.Sheets("Sheet2")

'Define the source and destination ranges
Dim rngSource As Range
Dim rngDestination As Range

Set rngSource = wsSource.Range("A1")
Set rngDestination = wsDestination.Range("A1")

'Copy the data from the source range to the destination range
rngDestination.CopyFromHere rngSource

'Find the next empty row in the destination worksheet
Dim lRow As Long
lRow = wsDestination.Cells(wsDestination.Rows.Count, 1).End(xlUp).Row + 1

'Select the next empty row
wsDestination.Rows(lRow).Select

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.