[英]How to check if Cell has Integer on it?
如何检查特定列是否在每个单元格上都有整数,如果它包含一个字符串,插入一个空白单元格到有问题的行。
Untested:未经测试:
Dim row As Long
Dim col As Long
col = 4 ' Whatever column you want to check
For row = 1 To 100 ' How many rows you want to check
If Not IsNumeric(Cells(row, col).Value) Then
' Do whatever you want to do in this case
End If
Next row
If you clarify what you mean by " Insert a blank cell to row in question ", I will try to update my solution.如果您通过“将空白单元格插入有问题的行”来澄清您的意思,我将尝试更新我的解决方案。
You can check even check with a forumla that the column contains no none-numbers only您甚至可以通过论坛检查该列是否仅包含非数字
=COUNTBLANK(AAA:AAA)-COUNTBLANK(B:B)=COUNT(B:B)
where I assume that column AAA:AAA is empty.我假设列 AAA:AAA 是空的。
A mix of the other answers for bigger data.大数据的其他答案的混合。 It first checks if the colomn has none Numbers only and if not, checks where.
它首先检查colomn 是否只有数字,如果没有,则检查哪里。
Dim row As Long
Dim LastRow As Long
LastRow = Range("B" & Rows.Count).End(xlUp).Row 'if you want the colomn B
If Excel.WorksheetFunction.CountBlank(Range("AAA:AAA")) - Excel.WorksheetFunction.CountBlank(Range("B:B")) = Excel.WorksheetFunction.Count(Range("B:B")) Then
For row = 1 To LastRow
If Not IsNumeric(Range("B" & row).Value) Then
' Do whatever you want to do in this case
End If
Next row
End if
Function IsInt(i As Variant) As Boolean
'checks if value i is an integer. returns True if it is and False if it isn't
IsInt = False
If IsNumeric(i) Then
If i = Int(i) Then
IsInt = True
End If
End If
End Function
Function IsString(s As Variant) As Boolean
'checks if variable s is a string, returs True if it is and False if not
IsString = True
If s = "" Then
IsString = False
Else
If IsNumeric(s) Then
IsString = False
End If
End If
End Function
Sub CheckInts(c As Integer)
'c = column number to check
'goes through all cells in column c and if integer ignores, if string sets to ""
Dim r, lastrow As Integer
lastrow = Sheet1.UsedRange.rows.Count 'find last row that contains data
For r = 1 To lastrow
If Not IsInt(Cells(r, c).Value) Then
If IsString(Cells(r, c).Value) Then
Cells(r, c).Value = ""
End If
End If
Next r
End Sub
then just call CheckInts passing in the column number you want to change eg CheckInts(2) will change column 2然后只需调用 CheckInts 传入您要更改的列号,例如 CheckInts(2) 将更改第 2 列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.