在exce中删除空行和空列的方法有很多,相对而言删除空行较为简单,只需进行筛选,将空白行筛选出来,删除即可,但要删除空列比较困难。因为你不能按列进行筛选删除。Excel中没有这个功能。当然你可以用另外一种方法,就是按列进行排序将空白列排序在一起,然后删除,但这种方法面临着列的顺序被打乱的问题,吃力不讨好。本文将利用Excel中强大VBA功能来完成。
删除空白行和列的方法:
步骤一、打开excel,按ALT+F11组合建,调出VBA程序窗口
步骤二、在插入菜单中,选择模块,插入一个模块
步骤三、在新建模块中的代码窗口将以下代码复制进去
‘删除空行
Sub DeleteEmptyRows()
Dim LastRow As Long, r As Long
LastRow = ActiveSheet.UsedRange.Rows.Count
LastRow = LastRow + ActiveSheet.UsedRange.Row -1
For r = LastRow To 1 Step -1
If WorksheetFunction.CountA(Rows(r)) = 0 Then Rows(r).Delete
Next r
End Sub
’删除空列
Sub DeleteEmptyColumns()
Dim LastColumn As Long, c As Long
LastColumn = ActiveSheet.UsedRange.Columns.Count
LastColumn = LastColumn + ActiveSheet.UsedRange.Column
For c = LastColumn To 1 Step -1
If WorksheetFunction.CountA(Columns(c)) = 0 Then Columns(c).Delete
Next c
End Sub
完成后关闭VBA窗口,选择保存,并保存Excel文件。
步骤四、保持步骤三中的excel文件处于打开状态,打开需要处理excel文件,执行上述两个宏即可。
用VBA删除空行和空列
在Excel中删除空行和空列的方法有很多,下面的方法是用VBA代码来删除工作表指定区域中的空行和空列:
Option Explicit
Sub Delete_Empty_Rows()
Dim rnArea As Range
Dim lnLastRow As Long, i As Long, j As Long
Application.ScreenUpdating = False
lnLastRow = Selection.Rows.Count
Set rnArea = Selection
j = 0
For i = lnLastRow To 1 Step -1
If Application.CountA(rnArea.Rows(i)) = 0 Then
rnArea.Rows(i).Delete
j = j + 1
End If
Next i
rnArea.Resize(lnLastRow - j).Select
Application.ScreenUpdating = True
End Sub
Sub Delete_Empty_Columns()
Dim lnLastColumn As Long, i As Long, j As Long
Dim rnArea As Range
Application.ScreenUpdating = False
lnLastColumn = Selection.Columns.Count
Set rnArea = Selection
新闻热点
疑难解答