[英]How to check if Cell has Integer on it?
如何檢查特定列是否在每個單元格上都有整數,如果它包含一個字符串,插入一個空白單元格到有問題的行。
未經測試:
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
如果您通過“將空白單元格插入有問題的行”來澄清您的意思,我將嘗試更新我的解決方案。
您甚至可以通過論壇檢查該列是否僅包含非數字
=COUNTBLANK(AAA:AAA)-COUNTBLANK(B:B)=COUNT(B:B)
我假設列 AAA:AAA 是空的。
大數據的其他答案的混合。 它首先檢查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
然后只需調用 CheckInts 傳入您要更改的列號,例如 CheckInts(2) 將更改第 2 列
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.