在操作数据时,空格、换行符、单引号、不可见字符会影响到我们对数据的处理,我们可以使用WPS EXCEL自带的工具进行清理。
开始-清理-特殊字符。
补充:
如果单元格中有公式,此种清理方式是不能清理公式的。如果有公式,那么计数时就会不准确,公式会计入计数,可使用如下方法进行清理:
在开发工具,visual vasic 编辑器中运行以下代码,可以清除所有为空的有公式的单元格,及公式错误的单元格。
Sub ClearHideCells()
Dim thisSht As Worksheet
Dim i As Long, j As Long, lastRow As Long, lastCol As Long
Dim thisShtName As String
Dim t
t = Timer
On Error Resume Next
'关闭弹窗和屏闪
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'获取当前工作表的名字
thisShtName = ActiveWindow.ActiveSheet.Name
Set thisSht = ActiveWorkbook.Sheets(thisShtName)
'获取最后一行一列
lastRow = ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1
lastCol = ActiveSheet.UsedRange.Column + ActiveSheet.UsedRange.Columns.Count - 1
'把主体加入数组
MsgBox Split(ActiveSheet.UsedRange.Address, "$")(3) & Split(ActiveSheet.UsedRange.Address, "$")(4)
arrMain = thisSht.Range("A1:" & Split(ActiveSheet.UsedRange.Address, "$")(3) & Split(ActiveSheet.UsedRange.Address, "$")(4))
'遍历数组
For i = 1 To lastRow
For j = 1 To lastCol
If arrMain(i, j) = "" Then
Cells(i, j).ClearContents
End If
Next
Next
'打开弹窗和屏闪
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "用时:" & Timer - t
End Sub
演示:
Excel清除空值公式和公式异常演示视频