簡體   English   中英

從一個單元格中獲取價值並將其集中在另一張表上 Vba Excel

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

我需要在另一個工作表中搜索一個元素,我認為我以某種方式應用了錯誤的方法,我無法激活單元格並聚焦該元素。 我在執行代碼時沒有收到任何錯誤在此處輸入圖像描述

在此處輸入圖像描述

我留下圖解的照片,以防我的想法和使用的代碼一起被理解得不太好

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

對於合並的單元格, Target.Count可能大於 1,但您不必擔心這一點,因為您不能一次雙擊多個單元格(除非合並)。

似乎 Target 作為整個合並范圍傳入,因此Value是一個數組:

在此處輸入圖像描述

您可以使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM