0

我希望能够双击第 1 列中的任何行并打开与该行对应的另一个电子表格。我让它在一张纸上工作,但我无法弄清楚如何让它在整个工作簿上工作。请帮忙

Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim fpath As String
    Dim fname As String
    Dim Tname As String
    Dim i As Integer

    Tname = ActiveSheet.Name

    If Target.Column = 1 Then
        If Target.Cells.Count > 1 Then Exit Sub
            i = Target.Column

            fpath = "Q:\Construction\Road\Patrols\" & Tname & "\"

            Select Case i
                Case 1: fname = ActiveCell.Value & ".xlsx"
            End Select

            If Dir(fpath & fname) = vbNullString Then
                MsgBox ("The file does not exist")
            Else
                ThisWorkbook.FollowHyperlink fpath & fname
            End If
        End If
    End If
End Sub
4

1 回答 1

0

您的代码位于特定工作表的 3 模块中。

要将其应用于整个工作簿,请将代码转移到ThisWorkbook模块,并使用Workbook_SheetBeforeDoubleClick事件

修改后的代码,进行了一些优化

Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    Dim fPath As String

    If Target.Column = 1 Then
        fPath = "Q:\Construction\Road\Patrols\" & Sh.Name & "\" & Target.Value & ".xlsx"
        If Dir(fPath) = vbNullString Then
            MsgBox ("The file does not exist")
        Else
            ThisWorkbook.FollowHyperlink fPath
        End If
    End If
End Sub
于 2012-07-02T01:24:44.447 回答