|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub 查询异常数据()
Application.ScreenUpdating = False
Dim lines, cols ''定义两个数组
Dim i As Integer, k As Integer ''数值型变量
Dim arr()
ReDim arr(1 To 10000, 1 To 3)
arr(1, 1) = "文件名称"
arr(1, 2) = "数据"
arr(1, 3) = "行号"
f = Dir(ThisWorkbook.Path & "\*.txt")
n = 1
Do While f <> ""
Open ThisWorkbook.Path & "\" & f For Input As #1 ''打开选择的文本文件
lines = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf) ''把文本文件的数据赋值给数组lines
k = UBound(lines) ' + 1 '文件的行数
'遍历每一行
Close #1 ''关闭打开的文本文件
For i = 2 To k ''循环数组行
If InStr(lines(i), ",") > 0 Then ''判断该行是否有逗号
cols = Split(lines(i), ",")(0) '以逗号作为分隔,将每行字符分割,分隔符可根据实际情况自己修改
sl = Len(Trim(cols))
If sl <> 11 Then
n = n + 1
arr(n, 1) = f
arr(n, 2) = cols
arr(n, 3) = i
End If
End If
Next i
f = Dir
Loop
If n = "" Then MsgBox "没有异常数据!": Exit Sub
With Sheets("sheet1")
.UsedRange.ClearContents ''清空原来的数据
.Columns("b:b").NumberFormatLocal = "@" ''设置A列为文本格式
.[a1].Resize(n, 3) = arr
End With
MsgBox "有" & n & "行数据异常"
Application.ScreenUpdating = True
End Sub
|
|