[英]Excel - VBA : How do I check if the last character of every single cell is > (superior sign)
How do I check in VBA-Excel whether the last character of every single cell of my sheet is a >
(superior sign). 如何在VBA-Excel中检查工作表中每个单元格的最后一个字符是否为>
(超级符号)。
Below is what I've been doing so far. 以下是到目前为止我一直在做的事情。
Dim c As Range
For Each c In Range("C1", Range(">" & Rows.Count).End(xlUp))
If Not IsNumeric(Mid(c, 3, 1)) Then
MsgBox ("Found !!!")
End If
Exit For
Next c
You can use right
function to get last character and check if it equals chr(62)
which is >
您可以使用right
函数获取最后一个字符,并检查它是否等于chr(62)
,即>
You can modify you code in a way below: 您可以通过以下方式修改代码:
Right(ActiveCell.Value, 1) = Chr(62)
It will return TRUE
if last character is >
如果最后一个字符是>
,它将返回TRUE
You can use the Like
operator with the wild-card *
as a prefix. 您可以将Like
运算符与通配符*
用作前缀。
Inside your For
loop, to check if a cell has a >
at the end, use: 在For
循环中,要检查单元格的末尾是否有>
,请使用:
Dim C As Range
With Worksheets("Sheet3") ' <-- modify "Sheet3" to your sheet's name
' loop through column C, from C1 until last cell with data in Column C (including blank cells in the middle)
For Each C In Range("C1:C" & .Cells(.Rows.Count, "C").End(xlUp).Row)
If C.Value Like "*>" Then '<-- if the end of the cell is ">"
MsgBox "Found !!! at Cell " & C.Address
End If
Next C
End With
First you use the active sheet. 首先,您使用活动表。 Then you try to find the last row in column "C". 然后,您尝试在列“ C”中找到最后一行。 Once you find it, you start looping over the range from C1 to C&lastRow. 找到它之后,就可以开始循环遍历从C1到C&lastRow的范围。 There, if you find the sign ">" on the right you print "found" and the row number in the immediate window. 在那里,如果在右侧找到符号“>”,则会在立即窗口中打印“找到”和行号。 Then you exit the sub. 然后退出子。
Here is the code: 这是代码:
Public Sub TestMe()
Dim c As Range
Dim lastRow As Long
With ActiveSheet
lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
For Each c In .Range(.Cells(1, 3), .Cells(lastRow, 3))
If Right(c, 1) = ">" Then
Debug.Print "FOUND"
Debug.Print c.Row
Exit For
End If
Next c
End With
End Sub
As I read your question title, you said that you want to check if the right most character in each cell in column C is ">". 当我阅读您的问题标题时,您说过要检查C列中每个单元格中最右边的字符是否为“>”。 Right? 对?
The I don't understand why you are using Exit For once a ">" is found any cell though it is used at wrong place as it will be triggered after first loop. 我不明白为什么您要使用Exit For一旦找到“ >>的任何单元格,尽管在错误的位置使用了它,因为它将在第一次循环后触发。
If you want to achieve what your question title says, try this... 如果您想实现问题标题的内容,请尝试此...
The following code will check each cell in column C and return a "Found!" 下面的代码将检查C列中的每个单元格并返回“ Found!”。 if the last character was a ">" or will return "Not Found!". 如果最后一个字符是“>”,否则将返回“未找到!”。
Sub CheckTheLastCharacterInString()
Dim lr As Long
lr = Cells(Rows.Count, "C").End(xlUp).Row
Range("D1:D" & lr).Value = Evaluate("IF(RIGHT(C1:C" & lr & ",1)="">"",""Found!"",""Not Found!"")")
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.