【问题标题】:Deleting rows in Excel based on criteria via Access VBA通过 Access VBA 根据条件删除 Excel 中的行
【发布时间】:2014-06-26 15:04:56
【问题描述】:

我正在开发一个模块,该模块将格式化 Excel 电子表格以导入 Access。电子表格来自源代码,其中包含 7 行标题数据、我需要的数据以及数据下方的 4 行乱码。到目前为止,我已经删除了标题信息:

Sub ExcelFormat()
Dim excelApp As Object
Dim excelWB As Object
Dim excelWS As Object
Set excelApp = CreateObject("Excel.Application")
Set excelWB = excelApp.workbooks.Open("Z:\Data\Test.xlsx")
excelApp.screenupdating = True
excelApp.Visible = True
Set excelWS = excelWB.worksheets("TestData")
excelWS.Rows("1:7").Delete

我无法选择 A 中的第一个空白单元格并将其删除以及它下面的 4 行。提前致谢。

【问题讨论】:

标签: excel ms-access vba


【解决方案1】:

首先删除最后4行,然后删除标题;

With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Rows(LastRow - 4 & ":" & LastRow).Delete
.Rows("1:7").Delete
End With

【讨论】:

    【解决方案2】:

    添加这个:

     dim i as long
    
     'loop through every row in the used range
     for i = 1 to excelWS.usedrange.rows.count
    
          'test if the cell in column A of the current row2 is blank
          If excelWS.cells(i,1)="" then
    
               'if the cell is blank, delete the row and the 4 rows beneath it, starting with the bottom most row
               excelWS.rows(i+4).delete
               excelWS.rows(i+3).delete
               excelWS.rows(i+2).delete
               excelWS.rows(i+1).delete
               excelWS.rows(i).delete
    
               'exit the loop
               Exit for
    
          end if
    
     next
    

    这将从第 1 行开始,遍历 A 中的每个单元格,直到找到一个空白单元格。然后它将删除该行及其下方的 4 行并停止。

    如果你只是想删除最后 5 行数据:

     dim i as long
    
     i = excelWS.usedrange.rows.count
    
     excelWS.usedrange.rows(i & ":" & i-4).delete
    

    【讨论】:

    • 仅最后五行的编辑答案
    猜你喜欢
    • 1970-01-01
    • 2014-11-09
    • 2011-11-30
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多