简体   繁体   English

Excel-VBA:如何检查每个单元格的最后一个字符是否>(超级符号)

[英]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.

相关问题 如何检查 Excel 单元中 Σ 字符的值? - VBA - How to check the value of Σ character in an Excel Cell? - VBA 如何检查单元格的内容是否为数组中的值-VBA Excel - How do I check if the contents of a cell is a value in an array - VBA Excel excel vba选择单元格中的倒数第二个字符 - excel vba select second to last character in a cell 如何检查VBA中的单元格是否为空白 - How do I check if a cell is blank in VBA Excel/VBA:如何将单行转换为单元格值数组 - Excel/VBA: How do I convert a single row into an array of the cell values 如何在excel vba中删除具有特定值的每个单元格? - How can I delete every cell with a specific value in excel vba? 如何将单元格的第一个字符与Excel中的另一个单元格组合? - How do I combine the first character of a cell with another cell in Excel? 如何在Excel中使用VBA删除文本字符串中单词的最后一个字符并插入到另一个单元格? - How to remove the last character of a word in a text string and insert to another cell using VBA in Excel? Excel / VBA-根据位置将多行合并为一-如何检查单元格值,然后将值保存在另一个单元格中? - Excel / VBA - Multiple Rows to One Based on Positioning - How do i check cell value then save the value in another cell? 如何停止导入每个Excel单元格的访问 - How Do I Stop Access From Importing Every Excel Cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM