我是VBA的初学者,我的传输数据代码遇到问题。当我在单元格(“ AD”列)中输入日期时,我试图自动将一行数据从一个表转移到另一张表底部的新行,但是当我尝试将数据转移到表格的最后一行下方的行。
xpath
任何帮助将不胜感激,因为我迷失了方向!
答案 0 :(得分:0)
如果我理解您的问题,可以尝试以下代码: 在激活sheet1的情况下执行宏
Sub TRANSFER_DATA()
Dim lastrow, i As Long
Dim ADCell as Integer
ADCell=30 ' control the column AD
'control how many data there are in column A. If you want count how many rows
'with ColumnAD change 1 in 30 (lastrow = Cells(rows.count,30).End(xlUp).Row)
lastrow = Cells(rows.count, 1).End(xlUp).Row
For i = 2 To lastrow
If Cells(i, ADCell) > 0 Then
rows(i & ":" & i).Select
Selection.Cut Worksheets("Sheet2").Range("A" & rows.count).End(xlUp).Offset(1)
End If
Next i
End Sub
我尝试了代码并起作用。
发表评论后更新帖子
Sub TRANSFER_DATA()
Dim lastrow, i, ls As Long
Dim ADCell as Integer
ADCell=30 ' control the column AD
'control how many data there are in column A. If you want count how many rows
'with ColumnAD change 1 in 30 (lastrow = Cells(rows.count,30).End(xlUp).Row)
lastrow = Cells(rows.count, 1).End(xlUp).Row
For i = 2 To lastrow
If Cells(i, ADCell) > 0 Then
rows(i & ":" & i).Select
Selection.Cut Worksheets("Sheet2").Range("A" & rows.count).End(xlUp).Offset(1)
End If
Next i
With Sheets("sheet2")
ls = .Cells(.rows.count, ADCell).End(xlUp).Row
.ListObjects("TableName").Resize Range("$A$1:$AD$" & ls)
End With
End Sub
更新的代码具有另一个变量ls。此变量具有sheet2的非空行数。 ListObjects。(“表名”)将新数据(行)插入表中。
我希望这对您有帮助