【问题标题】:Deleting Preceding Blank Columns and Rows删除前面的空白列和行
【发布时间】:2020-09-14 16:36:51
【问题描述】:

我正在尝试编写 VBA 代码来循环浏览活动工作簿中的每个工作表,并删除通向第一个包含数据的单元格的所有空白列和行。例如,如果第一个包含数据的单元格是 D5,则 A-C 列和第 1-4 行将被删除,而数据从 A1 开始。我有下面的代码,它适用于活动工作表,但我不知道如何让它循环通过其他工作表。


Sub DeleteRowsColumns()
' This will delete all Blank Columns and Rows before any data
    Dim ColCounter As Long
    Dim RowCounter As Long
    Dim SafeCount As Integer
    Dim ws As Worksheet
    SafeCount = 0
' Check Column A is empty if Yes then Delete till A is populated
    
For Each ws In ActiveWorkbook.Worksheets
    
    Do While ColCounter = 0
        SafeCount = SafeCount + 1
        ColCounter = Application.CountA(Columns(1).EntireColumn)
        If ColCounter = 0 Then
            Columns(1).EntireColumn.Delete
        End If
        If SafeCount = 50 Then
            Exit Do
        End If
    Loop

Next ws

' Check Row 1 is empty if Yes then Delete till 1 is populated

For Each ws In ActiveWorkbook.Worksheets
    
    SafeCount = 0
    Do While RowCounter = 0
        SafeCount = SafeCount + 1
        RowCounter = Application.CountA(Rows(1).EntireRow)
        If RowCounter = 0 Then
            Rows(1).EntireRow.Delete
        End If
        If SafeCount = 50 Then
            Exit Do
        End If
    'Loop

Next ws

MsgBox "Removed Preceding Blank Rows and Columns"

End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    在每个循环中,您需要指定要在哪个工作表上执行操作。只是循环并不能解决问题。例如:

        ColCounter = Application.CountA(ws.Columns(1).EntireColumn)
        If ColCounter = 0 Then
            ws.Columns(1).EntireColumn.Delete
    

    这可确保您使用正确的工作表。

    将其添加到循环中。

    For X = 1 To 50
        For i = 1 To 50
        ColCounter = Application.CountA(ws.Columns(i).EntireColumn)
            If ColCounter = 0 Then
                ws.Columns(i).EntireColumn.Delete
            End If
            rowCounter = Application.CountA(ws.Rows(i).EntireRow)
            If rowCounter = 0 Then
                ws.Rows(i).EntireRow.Delete
            End If
        Next i
    Next X
    

    【讨论】:

      【解决方案2】:

      您可以通过首先查找内容的开始位置(按行然后按列)来避免任何循环

      Sub RemoveEmpties()
          Dim f As Range, f2 As Range, ws As Worksheet
          For Each ws In ActiveWorkbook.Worksheets
              'first occupied cell on sheet (by row)
              Set f = ws.Cells.Find(What:="*", After:=ws.Cells(ws.Rows.Count, ws.Columns.Count), _
                          LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
                          SearchDirection:=xlNext, MatchCase:=False)
              
              If Not f Is Nothing Then
                  'have content, so find first-occupied column
                  Set f2 = ws.Cells.Find(What:="*", After:=ws.Cells(ws.Rows.Count, ws.Columns.Count), _
                                 LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, _
                                 SearchDirection:=xlNext, MatchCase:=False)
      
                  'remove rows/columns as required
                  If f.Row > 1 Then ws.Cells(1, 1).Resize(f.Row - 1).EntireRow.Delete
                  If f2.Column > 1 Then ws.Cells(1, 1).Resize(, f2.Column - 1).EntireColumn.Delete
              End If
          Next ws
      End Sub
      

      或者(同样只有最多两次删除):

      Sub RemoveEmpties2()
          Dim ws As Worksheet, r As Long, c As Long
          For Each ws In ActiveWorkbook.Worksheets
              'first make sure there's some content on the sheet...
              If Application.CountA(ws.Cells) > 0 Then
                  r = 1: c = 1
                  Do While Application.CountA(ws.Rows(r)) = 0
                      r = r + 1
                  Loop
                  If r > 1 Then ws.Rows(1).Resize(r - 1).Delete
                  Do While Application.CountA(ws.Columns(c)) = 0
                      c = c + 1
                  Loop
                  If c > 1 Then ws.Columns(1).Resize(, c - 1).Delete
              End If
          Next ws
      End Sub
      

      【讨论】:

        【解决方案3】:

        使用Find 方法

        流程

        • 在过程delFirstBlank 中定义了工作簿。声明了一个工作表变量。在下面的For Each Next 循环中,对于工作簿中的每个工作表,都会调用过程deleteFirstBlank。当循环退出时,会通过一个消息框通知用户代码已完成。
        • 在deleteFirstBlank 过程中,函数getFirstRow 的结果被写入一个变量。然后测试变量是否等于0,即工作表为空白。如果是,则退出该过程。如果不是,则测试变量是否大于1,即是否至少第一个 row 为空。如果是这样,则从第一个 row 到由减一的变量定义的 row 之间的 rows 将被删除。然后将函数getFirstRow 的结果写入一个变量,测试该变量是否大于1,即至少第一个列 是否为空。如果是这样,则删除从第一个column到由减一的变量定义的column的columns。
        • 在getFirstRow 过程(函数)中声明了一个范围变量。使用Find 方法,按rows 搜索,将在提供的工作表中找到的第一个非空白单元格(范围)分配给范围变量。如果Find 方法的结果是一个单元格区域,则其row 被写入函数的结果。如果不是,0 将作为结果写入,即工作表为空白。
        • 在getFirstColumn 过程(函数)中声明了一个范围变量。使用Find 方法,按columns 搜索,将在提供的工作表中找到的第一个非空白单元格(范围)分配给范围变量。如果Find 方法的结果是一个单元格区域,则其列 被写入函数的结果。如果不是,则将0 写入结果,即工作表为空白(后者永远不会发生,因为如果工作表在“getFirstRow”过程中为空白,则已经对其进行了测试)。

        守则

        Option Explicit
        
        Sub delFirstBlank()
            Dim wb As Workbook
            Set wb = ActiveWorkbook
            Dim ws As Worksheet
            For Each ws In wb.Worksheets
                deleteFirstBlank ws
            Next ws
            MsgBox "Removed first blank rows and columns.", vbInformation, "Success"
        End Sub
        
        Sub deleteFirstBlank(Sheet As Worksheet)
            Dim Current As Long
            Current = getFirstRow(Sheet)
            If Current = 0 Then GoTo ProcExit ' Blank sheet.
            If Current > 1 Then
                Sheet.Range(Sheet.Rows(1), Sheet.Rows(CLng(Current) - 1)).Delete
            End If
            Current = getFirstColumn(Sheet)
            If Current > 1 Then
                Sheet.Range(Sheet.Columns(1), Sheet.Columns(CLng(Current) - 1)).Delete
            End If
        ProcExit:
        End Sub
        
        Function getFirstRow(Sheet As Worksheet) As Long
            Dim rng As Range
            Set rng = Sheet.Cells.Find(What:="*", _
                                       After:=Sheet.Cells(Sheet.Rows.Count, _
                                                          Sheet.Columns.Count), _
                                       LookIn:=xlFormulas, _
                                       SearchOrder:=xlByRows)
            If Not rng Is Nothing Then
                getFirstRow = rng.Row
            Else
                getFirstRow = 0 ' Blank Sheet
            End If
        End Function
        
        Function getFirstColumn(Sheet As Worksheet) As Long
            Dim rng As Range
            Set rng = Sheet.Cells.Find(What:="*", _
                                       After:=Sheet.Cells(Sheet.Rows.Count, _
                                                          Sheet.Columns.Count), _
                                       LookIn:=xlFormulas, _
                                       SearchOrder:=xlByColumns)
            If Not rng Is Nothing Then
                getFirstColumn = rng.Column
            Else
                getFirstColumn = 0 ' Blank Sheet
            End If
        End Function
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-06-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-02-15
          • 1970-01-01
          相关资源
          最近更新 更多