VBA – 从每一行中剪下最后一个条目并粘贴到一个新列中。 行的长度变化

我是一个新用户,不能embedded图像…请看链接。

说我的数据看起来像这样: 以前 。 而且,我希望看起来像这样: After 。 这是我一直在尝试的代码(失败):

Dim lastRow As Long lastRow = Range("Sheet2!A" & Rows.Count).End(xlUp).Row Dim col As Integer col = Range("Sheet2!A1:A" & lastRow).End(xlToRight).Column Columns(col).Copy Columns(col + 1).Insert Shift:=xlToRight 

我希望macros在任意数量的行上处理数据集,因此是lastRow的东西。 有任何想法吗?

这工作怎么样?

 Sub move_data() Dim lastRow As Long, lastCol As Long Dim ws As Worksheet Set ws = Worksheets("Sheet2") lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row lastCol = ws.UsedRange.Columns.Count Dim i As Long For i = 1 To lastRow If ws.Cells(i, lastCol) = "" Then ws.Cells(i, ws.Cells(i, 1).End(xlToRight).Column).Cut ws.Cells(i, lastCol) End If Next i End Sub 

我犹豫使用UsedRange ,但如果你的数据确实存在于一个“块”应该工作正常。 基本上,它只是检查每一行,如果最后一列是空的,将最后一个单元格从列A移动到该列。