变量为活动单元格并将宏链接到超链接的宏

时间:2017-05-24 14:34:38

标签: excel vba excel-vba

所以我所拥有的是两个关于人的信息列表,一个列表是这些人的详细信息,另一个列表是他们的日程安排。因此,我想要做的是按计划我希望能够点击该人的姓名并弹出一个消息框并显示他们的所有详细信息。

到目前为止我的代码工作正常。我只是不知道在引用细节时如何将活动单元格实现为变量。

我不知道如何使这段代码动态化,以便当我点击其他链接时,它会根据我点击的人或地点提供详细信息。

Private Sub 
Worksheet_FollowHyperlink(ByVal target As Hyperlink)
Run ("details")

End Sub

Sub details()
Dim msg As String, i As Long, a As Variant
msg = ""
i = 2
    For Each a In Array("B", "D")
        msg = msg & Cells(i, a).Value & vbTab
    Next a
MsgBox msg
End Sub

图像显示点击“john”时会发生什么 我想要的是将超链接的“john”放在不同的页面上,并使该超链接成为动态的。

enter image description here

1 个答案:

答案 0 :(得分:0)

这里我有一个代码(我希望工作和)应该放在工作表中的时间表。

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

' Declare and set the variable to check if the event(dblclick)
' happened in the desired range - A.
Dim rIntersect As Range
Set rIntersect = Intersect(Target, ThisWorkbook.Sheets("SheetOfSchedule").Range("A:A"))
' Exits this sub if there is no change in the desired range.
If rIntersect Is Nothing Then Exit Sub

'cancel the doubleclick action.
Cancel = True

' Declare and set the variable that refers to their
' detials in the 'SheetOfDetails'.
Dim rEmployee As Range
Set rEmployee = LookFor(rIntersect.Value, ThisWorkbook.Sheets("SheetOfDetails"), 1)

' Declare the use the variable for looping
Dim iLoop As Integer
Dim strHolder As String     '<~ string that will hold the details
For iLoop = 1 To 3          '<~ change this to your requirements
  strHolder = strHolder & rEmployee.Offset(0, iLoop - 1).Value & vbTab
Next

MsgBox strHolder
End Sub

Private Function LookFor(NameOfEmployee As String, inSheet As Worksheet, inColumn As Integer) As Range
' This function will look for your employee name
' and return a range object where you can get
' details adjacent to this cell.
Dim lLoop As Long

With inSheet                                                '<~ make sure that we are working with the correct sheet
  For lLoop = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row     '<~ loops through the last row
    If .Cells(lLoop, inColumn).Value = NameOfEmployee Then  '<~ loop until you find the emp name
      Set LookFor = .Cells(lLoop, inColumn)                 '<~ set the function to return that cell
      Exit Function                                         '<~ exit to save from
    End If                                                  '   unnecesary loopings.
  Next
End With

End Function