2017-12-21 8 views
1

아래 코드는 문자열이 작을 때 다른 워크 시트에서 셀을 찾기 위해 완벽하게 작동하지만 큰 텍스트 문자열은 오류를 발생시킵니다. 오류가 발생했을 때 VBA 창을 열지 않고 단지 MsgBox을 제공하기 위해 오류 처리를 시도했습니다.더 큰 255자를 포함하는 셀 찾기

아무도 도와 주실 수 있습니다. 많은 문자가있는 셀을 찾거나 가능하지 않은 경우 과 같이 말하면서 오류 처리기를 넣으시면을 검색 할 수 있습니다.

코드의 역할은 각 셀에 텍스트가있는 셀 범위입니다. 해당 셀 또는 오른쪽의 셀 2 열을 클릭 한 다음 FIND 단추를 클릭하여 다음 워크 시트에서 동일한 셀 값을 찾을 수 있습니다. 모든 셀은 고유합니다. 당신을 또한

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 

: 가치

당신이 할당 한 각 문 다음에이 추가 값을 :

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

을,하지만 당신은'당신의 두 블록 코드의 모두'값 = ActiveCell.EntireRow.Range ("E1")를 사용할 수 있습니다

코드는 다음과 같이 작동합니다 따라서 두 블록을 하나의 블록으로 결합합니다. – YowE3K

+2

위대한 아이디어, 나는 내가 갈 수 있다는 것을 깨닫지 못했다. 그러나 그 충고가 실제로 도움이된다. 그런 식으로 현재 셀이 행의 모든 ​​셀에있을 수 있지만 E 열의 셀을 검색하고 코드의 크기를 반으로 줄입니다. 감사합니다. –

답변

0

너무 오래 사용하면 다음을 수행 할 수있는 텍스트를 나타내는 오류 메시지를 제공하기 위해 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 문이 메모 인 경우 더 나은 구조는 다음과 같습니다.

변경이 코드

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