查找包含255个字符的单元格

问题描述 投票:1回答:1

下面的代码非常适合在字符串很小的情况下在不同的工作表上查找单元格,但是大文本字符串会出错。我尝试使用错误处理,甚至只是给它一个MsgBox而不是在它出错时打开一个VBA窗口。

任何人都可以提供帮助,最好找到包含许多字符的单元格,如果不可能的话,可以使用错误处理程序来表示类似,太大而无法搜索。

代码的作用是,每个单元格中都有一系列带有文本的单元格。我可以单击该单元格,或单击右侧的单元格2列,然后单击FIND按钮,进入下一个工作表以查找完全相同的单元格值。所有细胞都是独特的。

Sub Find_Cell()

    Dim NA As Worksheet
    Set NA = Worksheets("Notes Analysis")

    LastRow = NA.Cells(Rows.Count, 2).End(xlUp).Row

    On Error Resume Next
    If Not Intersect(ActiveCell, Range("G19:G" & LastRow)) Is Nothing Then

        Dim value As String                      'Declare a string
        value = ActiveCell.Offset(, -2)          'Get the value of the selected Cell
        Dim ws As Worksheet

        'ws is the worksheet from we are searching the value
        'You have to change myWorkSheetName for you worksheet name
        Set ws = ThisWorkbook.Worksheets("DEBT_SALE_ACTIVITY")

        ws.Activate
        Dim c As Range                           'Declare a cell
        Set c = ws.Cells.Find(value, LookIn:=xlValues) 'Search the value

        If Not c Is Nothing Then                 'If value found
            c.Activate                           'Activate the cell, select it
        Else
            MsgBox "Not found"                   'shows a message "Not Found"
        End If
    Else

        If Not Intersect(ActiveCell, Range("E19:E" & LastRow)) Is Nothing Then

            Dim value2 As String                 'Declare a string
            value2 = ActiveCell                  'Get the value of the selected Cell
            Dim ws2 As Worksheet

            'ws is the worksheet from we are searching the value
            'You have to change myWorkSheetName for you worksheet name
            Set ws2 = ThisWorkbook.Worksheets("DEBT_SALE_ACTIVITY")

            ws2.Activate

            Dim c2 As Range                      'Declare a cell
            Set c2 = ws2.Cells.Find(value2, LookIn:=xlValues) 'Search the value

            If Not c2 Is Nothing Then            'If value found
                c2.Activate                      'Activate the cell, select it
            Else
                MsgBox "Not found"               'shows a message "Not Found"
            End If

        Else

            MsgBox "Select an Account Note"

        End If                                   'end the If for if active cell is in our notes
    End If                                       'end the If for if active cell is in Account note

End Sub
excel vba excel-vba
1个回答
0
投票

要提供指示文本太长的错误消息,您可以执行以下操作:

在为每个语句赋值后为其值添加此值:

value = ActiveCell.Offset(, -2) 'Get the value of the selected Cell
If Len(value) > 255 Then
    MsgBox "Text in cell " & CStr(ActiveCell.Address) & " is too long", vbOKOnly, "Search Text Too Long"
    Exit Sub
End If

此外,您可能想要更改if ... then ... else代码结构。

目前您的代码运行方式如下:

If Not Intersect(ActiveCell, Range("G19:G" & LastRow)) Is Nothing Then 
    do things
    exit sub
Else
    If Not Intersect(ActiveCell, Range("E19:E" & LastRow)) Is Nothing Then
        do things
        exit sub
    Else 
        MsgBox "Select an Account Note" 
        exit sub

哪个,基于您对结束的评论如果不是您的消息框所说的。如果你的第一个if语句是Account Notes而你的第二个if语句是notes,那么更好的结构如下。

更改此代码

Else

    If Not Intersect(ActiveCell, Range("E19:E" & LastRow)) Is Nothing Then

看起来像这样

ElseIf Not Intersect(ActiveCell, Range("E19:E" & LastRow)) Is Nothing Then

然后声明`MsgBox'选择一个帐户注释“将是准确的。您还可以删除一个End If语句。

您的代码将按以下方式运行:

If Not Intersect(ActiveCell, Range("G19:G" & LastRow)) Is Nothing Then 
    do things
    exit sub
ElseIf Not Intersect(ActiveCell, Range("E19:E" & LastRow)) Is Nothing Then
    do things
    exit sub
Else 
    MsgBox "Select an Account Note" 
    exit sub
© www.soinside.com 2019 - 2024. All rights reserved.