Excel 嗨,我';我目前正试图在VBA中创建一个宏,该宏将查找特定行中的所有单元格,但它不会';不包含数字
如何在for循环中使用MID函数,或者是否有更好的替代方法?? 我目前正在尝试创建一个宏来查找所有单元格(在一列中),该宏不包含数字(它们是数字和文本的混合体)。然后,它应该为这些单元格指定一种内部颜色,为其余单元格指定另一种内部颜色。即使是很小的提示也会有很大的帮助:)Excel 嗨,我';我目前正试图在VBA中创建一个宏,该宏将查找特定行中的所有单元格,但它不会';不包含数字,excel,vba,Excel,Vba,如何在for循环中使用MID函数,或者是否有更好的替代方法?? 我目前正在尝试创建一个宏来查找所有单元格(在一列中),该宏不包含数字(它们是数字和文本的混合体)。然后,它应该为这些单元格指定一种内部颜色,为其余单元格指定另一种内部颜色。即使是很小的提示也会有很大的帮助:) 您可以使用len()动态使用单元格值的长度,而不是使用75 由于要查找不包含数字的单元格,因此可以使用布尔(真/假)变量存储currentCell值是否包含数字。然后画细胞 Sub FindIDs() Dim i
您可以使用
len()
动态使用单元格值的长度,而不是使用75
由于要查找不包含数字的单元格,因此可以使用布尔(真/假)变量存储currentCell
值是否包含数字。然后画细胞
Sub FindIDs()
Dim i As Integer
Dim j As Integer
Dim ColumnA As Integer
Dim CurrentCell As String
Dim hasNoNumber as Boolean
ColumnA = Application.WorksheetFunction.CountA(Range("A:A"))
For i = 2 To ColumnA
CurrentCell = Cells(i, 1)
hasNoNumber=True
For j = 1 To len(CurrentCell)
If Isnumeric(Mid(CurrentCell, j, 1)) Then
'We found a number, flip the flag
hasNoNumber=False
'Because we found a number, then exit the for loop
Exit For
End If
Next j
'Now set the color:
If hasNoNumber
Cells(i, 1).Interior.Color = RGB(50, 200, 30)
Else
Cells(i, 1).Interior.Color = RGB(174, 240, 194)
End If
Next i
End Sub
最后,您可能会发现,通过创建一个函数来告诉您字符串是否包含数字,从而分离出这里的逻辑很有帮助。这样,如果您需要再次执行此逻辑,您可以调用该函数,而不是到处复制和粘贴代码。它还使您更容易了解主子程序中的内容
Sub FindIDs()
Dim i As Integer
Dim j As Integer
Dim ColumnA As Integer
ColumnA = Application.WorksheetFunction.CountA(Range("A:A"))
For i = 2 To Column
'Set the color based on whether there is a number in the string
If hasNoNumber(cells(i,1).value)
Cells(i, 1).Interior.Color = RGB(50, 200, 30)
Else
Cells(i, 1).Interior.Color = RGB(174, 240, 194)
End If
Next i
End Sub
Function HasNoNumber(strData As String) As Boolean
Dim intChar As Integer
HasNoNumber = True
For intChar = 1 To Len(strData)
If IsNumeric(Mid(strData, intChar, 1)) Then
HasNumber = False
Exit Function
End If
Next intChar
End Function
您可以使用
len()
动态使用单元格值的长度,而不是使用75
由于要查找不包含数字的单元格,因此可以使用布尔(真/假)变量存储currentCell
值是否包含数字。然后画细胞
Sub FindIDs()
Dim i As Integer
Dim j As Integer
Dim ColumnA As Integer
Dim CurrentCell As String
Dim hasNoNumber as Boolean
ColumnA = Application.WorksheetFunction.CountA(Range("A:A"))
For i = 2 To ColumnA
CurrentCell = Cells(i, 1)
hasNoNumber=True
For j = 1 To len(CurrentCell)
If Isnumeric(Mid(CurrentCell, j, 1)) Then
'We found a number, flip the flag
hasNoNumber=False
'Because we found a number, then exit the for loop
Exit For
End If
Next j
'Now set the color:
If hasNoNumber
Cells(i, 1).Interior.Color = RGB(50, 200, 30)
Else
Cells(i, 1).Interior.Color = RGB(174, 240, 194)
End If
Next i
End Sub
最后,您可能会发现,通过创建一个函数来告诉您字符串是否包含数字,从而分离出这里的逻辑很有帮助。这样,如果您需要再次执行此逻辑,您可以调用该函数,而不是到处复制和粘贴代码。它还使您更容易了解主子程序中的内容
Sub FindIDs()
Dim i As Integer
Dim j As Integer
Dim ColumnA As Integer
ColumnA = Application.WorksheetFunction.CountA(Range("A:A"))
For i = 2 To Column
'Set the color based on whether there is a number in the string
If hasNoNumber(cells(i,1).value)
Cells(i, 1).Interior.Color = RGB(50, 200, 30)
Else
Cells(i, 1).Interior.Color = RGB(174, 240, 194)
End If
Next i
End Sub
Function HasNoNumber(strData As String) As Boolean
Dim intChar As Integer
HasNoNumber = True
For intChar = 1 To Len(strData)
If IsNumeric(Mid(strData, intChar, 1)) Then
HasNumber = False
Exit Function
End If
Next intChar
End Function
您可以执行以下操作: 首先创建一个函数来验证字符串的条件:
Function ContainsNumber(s As String) As Boolean
Dim i As Long
For i = 1 To Len(s)
If IsNumeric(Mid(s, i, 1)) Then
ContainsNumber = True
Exit Function
End If
Next i
End Function
如果字符串包含数字,则返回True
,否则返回False
(默认布尔值)
然后--只需在目标范围内使用条件格式:
我得到这个的方法是输入条件格式规则
=ContainsNumber(A1)
(不是$A$1),然后使用格式画师将其应用于整个范围。您应该能够针对您的问题对此进行调整。您可以执行以下操作: 首先创建一个函数来验证字符串的条件:
Function ContainsNumber(s As String) As Boolean
Dim i As Long
For i = 1 To Len(s)
If IsNumeric(Mid(s, i, 1)) Then
ContainsNumber = True
Exit Function
End If
Next i
End Function
如果字符串包含数字,则返回True
,否则返回False
(默认布尔值)
然后--只需在目标范围内使用条件格式:
我得到这个的方法是输入条件格式规则
=ContainsNumber(A1)
(不是$A$1),然后使用格式画师将其应用于整个范围。您应该能够针对您的问题对此进行调整。您还可以使用:
您还可以使用:
您希望在此行
单元格(i,1).Interior.Color=RGB(174,240,194)
之后的IF中使用退出按钮,这样,如果它找到一个数字,它会更改颜色并停止从其他方向看颜色将仅取决于最后一个字符。另外,将75
更改为Len(CurrentCell)
如果要在这一行单元格(i,1.Interior.Color=RGB(174,240,194)之后的IF中使用退出,则如果找到一个数字,它会更改颜色并停止从其他方向查看,颜色将仅取决于最后一个字符。同时将75
更改为Len(CurrentCell)
使用常规表达的好主意感谢所有可爱的评论、想法和帮助!我会看一看这一切,试着让我的宏按预期工作:)使用常规表达的好主意谢谢所有可爱的评论、想法和帮助!我会看一看这一切,并尝试使我的宏按预期工作:)