简体   繁体   中英

Get Value from one cell and focus it on another sheet Vba Excel

I need to search for an element in another sheet, I consider that I am applying the wrong method in some way, I cannot activate the cell and focus that element. I don't receive any error executing the code在此处输入图像描述

在此处输入图像描述

I leave illustrated photos in case that my idea is not understood very well along with the code used

If Target.Count = 1 Then
       If Not Intersect(Target, Me.Range("C:C")) Is Nothing Then
           Set wsSearch = ThisWorkbook.Sheets("TARJETAS")
           Set f = wsSearch.Columns("B:B").Find( _
                             what:=Target.Value, lookat:=xlWhole)
           If Not f Is Nothing Then
               Cancel = True
               wsSearch.Activate
               f.Select
           End If
       End If
   End If

With merged cells then Target.Count may be >1, but you don't need to worry about that because you can't double-click more than one cell at once (unless merged).

Seems like Target gets passed in as the whole merge range, so the Value is an array:

在此处输入图像描述

You can get the Value to search for using Target(1,1) :

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim wsSearch As Worksheet, f As Range, v
    Debug.Print Target.Count

    If Not Intersect(Target, Me.Range("C:C")) Is Nothing Then
        v = Target(1, 1).Value '<<< get the value
        Set wsSearch = ThisWorkbook.Sheets("TARJETAS")

        Debug.Print "Searching for " & v
        Set f = wsSearch.Columns("B:B").Find(what:=v, lookat:=xlWhole)
        If Not f Is Nothing Then
            Debug.Print "Found at " & f.Address()
            Cancel = True
            wsSearch.Activate
            f.Select
        End If
    End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM