我正在开发一个模块,用于格式化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行时遇到问题。提前谢谢。
答案 0 :(得分:0)
添加:
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
答案 1 :(得分:0)
首先删除最后4行,然后删除标题;
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Rows(LastRow - 4 & ":" & LastRow).Delete
.Rows("1:7").Delete
End With