[英]Delete rows in a specific range that contains blank values
I'm trying to delete the rows in a specified cell range that contains blank values. 我正在尝试删除包含空白值的指定单元格区域中的行。
Tried this: 试过这个:
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
I'm getting an error in the line 我在一行中遇到错误
objExcel.Sheets("FNR_CHECK").Range("E3:G13").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Saying subscript out of range 说下标超出范围
Define a Range
and use the SpecialCells()
function to select blank cells within that range. 定义一个Range
并使用SpecialCells()
函数选择该范围内的空白单元格。 Then, apply the EntireRow
property to select and delete the rows. 然后,应用EntireRow
属性以选择和删除行。
For example, to delete the rows that have blanks in column A: 例如,要删除A列中包含空白的行:
Const xlCellTypeBlanks = 4
objExcel.ActiveSheet.Range("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Here is small sub which will loop through the column A (Change it to your selected column) and it will first trim the cell value in case if you have space or anything and then it will delete the entire row if blank value is found. 这是一个小型子控件,它将在A列中循环(将其更改为选定的列),如果有空格或其他任何内容,它将首先修剪单元格的值,如果找到空白值,它将删除整行。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.