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

时间:2017-12-21 02:39:46

标签: excel vba excel-vba

我的代码可以很好地在字符串很小的情况下在不同的工作表上找到一个单元格,但是大文本字符串会产生错误。我尝试使用错误处理,即使只是提供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

1 个答案:

答案 0 :(得分: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