繁体   English   中英

宏,变量是活动单元格,并将宏链接到超链接

[英]macro with variable being active cell and linking the macro to a hyperlink

因此,我有两个关于人员的信息列表,一个列表是这些人员的详细信息,另一个是他们的日程安排。 因此,我要按照时间表进行操作,我希望能够单击此人的姓名,并弹出一个消息框并显示其所有详细信息。

到目前为止,我的代码有效。 我只是不知道在引用详细信息时如何将活动单元实现为变量。

我不知道如何使此代码动态化,因此当我单击其他链接时,它会根据单击的对象或位置提供详细信息。

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”放在不同的页面上并使该超链接动态化。

在此处输入图片说明

在这里,我有一个代码( 我希望能够工作,并且 )应该放在计划表所在的工作表中。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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