gpt4 book ai didi

excel - 在excel的列中搜索字母字符

转载 作者:行者123 更新时间:2023-12-04 20:16:00 26 4
gpt4 key购买 nike

我有许多来自其他来源的 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

关于excel - 在excel的列中搜索字母字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25793798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com