VBA将每隔一行切换到列的最后一行并粘贴到新列,然后删除空行

希望能得到一些帮助。 我是VBA的新手,特别是循环。 我得到的数据长度不同,它将在A列中包含错误string(A1,A3,A5,…)和相应的错误消息(A2,A4,A6,…)。

我想剪切错误信息数据(A2,A4,A6,…)并将其粘贴到列H(H1,H3,H5,…)中,直到列A中的最后一行数据。然后像新清空的行(2,4,6,…)被删除,所以我有一个错误string在列AI表可以使用“文本到列”上,并在列H中相应的错误消息。

最终目标是在做一个“文本到列”之后,我将有一块数据,我可以变成一张表。

如果你认为有更好的方法比剪切粘贴删除空行方法,那么我都是耳朵。 如果你有一个很好的资源来构build循环的X代码到最后一排,我也喜欢打扰你。

Sub FormatForErrorCorrections() Dim i As Integer 'i always stores the current row# that the loop is operating on 'since we'll be deleting rows, start at the bottom and work our way up With ActiveSheet For i = .Range("A" & .Rows.Count).End(xlUp).Row To 1 Step -2 'every other row 'assign value from column A to previous line column H .Range("H" & (i - 1)).Value = .Range("A" & i).Value 'split A column into A:E using "." delimiter .Range("A" & (i - 1) & ":E" & (i - 1)) = Split(.Range("A" & (i - 1)).Value, ".") 'delete the current row .Rows(i).Delete Next i 'i decrements by 2 until we reach 1 (Step -2) End With End Sub 

使用VBA和循环是一种可能性。 另一个是使用帮助列来sorting数据。

  • 在表中添加一个辅助列,并在前两个单元格中input“a”和“b”。
  • select两个单元格,然后双击填充句柄以将序列填充到表格的最后一行。
  • 按新列对表格进行sorting
  • select所有“a”行中的单元格并复制
  • 粘贴在“b”行旁边
  • 删除帮手列