我一直在学习 VBA 代码,但这让我很为难。我使用的是 Excel-2013。
我的“发票”表中有 4 个单元格(单元格 E3、C3、E36 和 E34),我想将它们转移到下一个可用行中的“付款方式”表中。可用的第一行是第 3 行。我希望单元格 E3 复制到 C 列,C3 复制到 D 列,E36 复制到 E 列,E34 复制到 F 列。保存每张发票时,我希望传输这些数据。我已经有代码清除我的发票。我只需要传输这 4 个数据单元格。
答案1
请尝试以下操作:
Sub CopyDataToMethodOfPayment()
Dim NewRow: NewRow = GetFirstEmptyRowOnMethodOfPayment 'Find the next row free on "Method Of Payment"
Worksheets("Method Of Payment").Cells(NewRow, 3).Value = Worksheets("Invoice").Range("E3").Value 'Copy E3 to Column C on empty row
Worksheets("Method Of Payment").Cells(NewRow, 4).Value = Worksheets("Invoice").Range("C3").Value 'Copy C3 to Column D on empty row
Worksheets("Method Of Payment").Cells(NewRow, 5).Value = Worksheets("Invoice").Range("E36").Value 'Copy E36 to Column E on empty row
Worksheets("Method Of Payment").Cells(NewRow, 6).Value = Worksheets("Invoice").Range("E34").Value 'Copy E34 to Column F on empty row
End Sub
Function GetFirstEmptyRowOnMethodOfPayment() As Integer
Dim RowCount: RowCount = 1 'Set row to start looking for empty rows from
Do
RowCount = RowCount + 1 'Increment to next row
Loop Until IsEmpty(Worksheets("Method Of Payment").Cells(RowCount, 3).Value) 'Stop once we find an empty one
GetFirstEmptyRowOnMethodOfPayment = RowCount 'Return the row number of the empty one
End Function