删除包含空白值的特定范围中的行
我试图删除包含空值的指定单元格区域中的行。
试过这个:
sub TestDeleteRow() Set exlTest = objExcel.Workbooks.Open(strPathExc) objExcel.Application.Visible = True objExcel.Sheets("FNR_CHECK").Range("E3:G13").SpecialCells(xlCellTypeBlanks).EntireRow.Delete Msgbox "Rows deleted", vbOKOnly, "TA Balance Sheet" end sub
我收到了一个错误
objExcel.Sheets("FNR_CHECK").Range("E3:G13").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
下标超出范围
定义一个Range
并使用SpecialCells()
函数来select该范围内的空白单元格。 然后,应用EntireRow
属性来select和删除行。
例如,要删除A列中有空白的行:
Const xlCellTypeBlanks = 4 objExcel.ActiveSheet.Range("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
这里是小的子将循环通过列A(将其更改为您select的列),它将首先修剪单元格的值,如果您有空间或任何东西,然后它将删除整个行,如果find空白值。
Sub DeleteRowsThatLookEmpty() Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Dim Rng As Range, ix As Long Set Rng = Intersect(Range("A:A"), ActiveSheet.UsedRange) 'You can specify the sheet you want and change the column For ix = Rng.Count To 1 Step -1 If Trim(Replace(Rng.Item(ix).Text, Chr(160), Chr(32))) = "" Then Rng.Item(ix).EntireRow.Delete End If Next Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True End Sub