如何检查Cell是否有整数?

时间:2009-12-10 13:50:03

标签: excel vba excel-vba

如何检查特定列是否在每个单元格上都有整数,如果它包含字符串,请将空白单元格插入到相关行中。

4 个答案:

答案 0 :(得分:3)

未测试:

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

如果您通过“将空白单元格插入问题行”阐明您的意思,我将尝试更新我的解决方案。

答案 1 :(得分:0)

您甚至可以使用公式检查该列是否仅包含非数字

=COUNTBLANK(AAA:AAA)-COUNTBLANK(B:B)=COUNT(B:B)

我假设列AAA:AAA为空。

答案 2 :(得分:0)

更大数据的其他答案的混合。 它首先检查colomn是否只有Numbers,如果没有,检查在哪里。

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

答案 3 :(得分:0)

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   

然后只需调用CheckInts并传递您要更改的列号 例如CheckInts(2)将更改第2列