【问题标题】:Deleting empty rows in Excel after they have been copied into new worksheet using VBA使用 VBA 将空行复制到新工作表后删除 Excel 中的空行
【发布时间】:2017-06-17 02:24:02
【问题描述】:

我已成功为 Excel 编写了一个 VBA 脚本,该脚本检查 A 列是否包含特定条目(在本例中为:2016),然后将整行复制到新工作表中。

唯一的问题是它将行复制到与原始工作表中完全相同的位置。因此,我在两者之间得到空行。我希望宏在复制这些空行后立即删除它们,或者将这些行一个接一个地复制到新工作表中。

Sub CopyRow()

Application.ScreenUpdating = False

Dim x As Long
Dim MaxRowList As Long
Dim S As String
Dim wsSource As Worksheet
Dim wsTarget As Worksheet


Set wsSource = ThisWorkbook.Worksheets("Tab 1")
Set wsTarget = ThisWorkbook.Worksheets("Tab 2")

aCol = 1
MaxRowList = wsSource.Cells(rows.Count, aCol).End(xlUp).Row

For x = 2 To MaxRowList
    If InStr(1, wsSource.Cells(x, 1), "2016") Then
    wsTarget.rows(x).Value = wsSource.rows(x).Value
    End If
Next

Application.ScreenUpdating = True

End Sub

感谢任何帮助。提前致谢。

【问题讨论】:

  • 1.您可以为导出行保留一个单独的行计数器 2. 您可以使用 .end(xlUp) (谷歌如何找到列中的最后一行)但请注意,这样做您将失去检测您是否已经已经复制了这些行。如果你用#2 运行宏两次,那么你会得到重复。如果您使用行计数器进行导出,那么您可以根据需要多次覆盖。

标签: vba excel rows


【解决方案1】:

您可以像这样为目标行设置一个变量:

Sub CopyRow()

Application.ScreenUpdating = False

Dim x As Long
Dim MaxRowList As Long
Dim S As String
Dim wsSource As Worksheet
Dim wsTarget As Worksheet


Set wsSource = ThisWorkbook.Worksheets("Tab 1")
Set wsTarget = ThisWorkbook.Worksheets("Tab 2")

aCol = 1
MaxRowList = wsSource.Cells(rows.Count, aCol).End(xlUp).Row

destiny_row = 2 
For x = 2 To MaxRowList
    If InStr(1, wsSource.Cells(x, 1), "2016") Then
    wsTarget.rows(destiny_row).Value = wsSource.rows(x).Value
    destiny_row = destiny_row +1
    End If
Next

Application.ScreenUpdating = True

End Sub

这样,它将开始复制目标工作表第 2 行中的这些值,并将根据 if 条件增加。告诉我它是怎么回事...

【讨论】:

  • 这很好用!非常感谢。你能帮我进一步吗?我还需要将完整的第一行从工作表 1 复制到工作表 2 的第 1 行。谢谢。 :)
  • 很高兴它有帮助,当然,您只需要使用“wsSource.rows(x).EntireRow.Copy”,然后在目标中使用 .paste。如果对您有帮助,记得将我的答案标记为正确。
【解决方案2】:
Sub CopyRow()

    Application.ScreenUpdating = False

    Dim x As Long
    Dim MaxRowList As Long, PrintRow as Long
    Dim S As String
    Dim wsSource As Worksheet
    Dim wsTarget As Worksheet


    Set wsSource = ThisWorkbook.Worksheets("Tab 1")
    Set wsTarget = ThisWorkbook.Worksheets("Tab 2")

    aCol = 1
    MaxRowList = wsSource.Cells(rows.Count, aCol).End(xlUp).Row

    For x = 2 To MaxRowList
        If InStr(1, wsSource.Cells(x, 1), "2016") Then
            PrintRow = wsTarget.range("A" & wsTarget.rows.count).end(xlup).row
            wsTarget.rows(PrintRow).Value = wsSource.rows(x).Value
        End If
    Next

    Application.ScreenUpdating = True

End Sub

【讨论】:

  • 由于某种原因,它没有复制 A 列中包含 2016 的所有行。它只复制了一个。在我的工作表中,我在 A 列中有 2 个 2016 条目(它们在 A3 和 A7 中)。
【解决方案3】:

您可以使用AutoFilter 方法,这样您就无需使用For 循环遍历所有行,只需将整个过滤范围复制到“Tab 2”工作表即可。

代码(cmets内的解释)

Option Explicit

Sub CopyRow()

Application.ScreenUpdating = False

Dim x As Long
Dim MaxRowList As Long
Dim MaxCol As Long

Dim S As String
Dim aCol As Long
Dim wsSource As Worksheet
Dim wsTarget As Worksheet
Dim SourceRng As Range
Dim VisRng As Range
Set wsSource = ThisWorkbook.Worksheets("Tab 1")
Set wsTarget = ThisWorkbook.Worksheets("Tab 2")

aCol = 1

With wsSource
    MaxRowList = .Cells(.Rows.Count, aCol).End(xlUp).Row ' find last row
    MaxCol = .Cells(1, .Columns.Count).End(xlToLeft).Column ' find last column

    Set SourceRng = .Range(.Cells(1, 1), .Cells(MaxRowList, MaxCol)) ' set source range to actually occupied range

    .Range("A1").AutoFilter ' use AutoFilter method
    SourceRng.AutoFilter Field:=1, Criteria1:="2016"

    Set VisRng = SourceRng.SpecialCells(xlCellTypeVisible) ' set range to filterred range

    VisRng.Copy ' copy entire visible range
    wsTarget.Range("A2").PasteSpecial xlPasteValues ' past with 1 line
End With

Application.ScreenUpdating = True

End Sub

【讨论】:

  • 我在我的工作表上尝试了此代码,但它删除了工作表 1 中除第 1 行之外的所有条目。它对工作表 2 没有任何作用。
  • @D.Todor 已删除?我的代码中没有我删除的地方
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-22
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
相关资源
最近更新 更多