我有许多其他来源的excel文件,并且需要检查它们是否没有在数字字段中意外地添加字母字符。并非所有变量都只是数字变量,我不能简单地清除字母字符。
如何标记这些情况,以便引起发送数据的人员的注意?
这段代码可以满足我的要求。将其添加到VBA编辑器中的普通模块,然后运行名为catchmixedvals的宏
该代码假定以下内容(但您可以更改这些内容以适合):
数据位于名为“数据”的工作表中
数据从“ A3”单元格开始
包含“混合”数据的单元格将填充为红色
数据保持不变
Sub catchmixedvals()
Dim ws1 As Worksheet
Dim stRow As Long, stCol As Long
Dim enRow As Long, enCol As Long
Dim drng As Range
Dim cl As Range
Set ws1 = Worksheets("Data")
stRow = 3
stCol = 1
With ws1
'determine data range
enRow = .Cells(.Rows.Count, stCol).End(xlUp).Row
enCol = .Cells(stRow, .Columns.Count).End(xlToLeft).Column
Set drng = Range(.Cells(stRow, stCol), .Cells(enRow, enCol))
'examine each cell in data range
For Each cl In drng
On Error Resume Next
If IsNumeric(cl) Then
'do nothing its a number
Else
'a text cell therefore check for digits
For i = 1 To Len(cl)
If Mid(cl, i, 1) >= "0" And Mid(cl, i, 1) <= "9" Then
cl.Interior.ColorIndex = 3
Exit For
End If
Next
End If
Next cl
End With
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句