Running VBA from a HYPERLINK()

前端 未结 4 1340
生来不讨喜
生来不讨喜 2020-11-30 13:43

I\'m trying to let my users send email easily from their Excel spreadsheets, which they like to use as front-ends for data entry and manipulation. My intention is to write a

相关标签:
4条回答
  • 2020-11-30 13:51

    The body of your email doesn't look excessively long for use with the mailto hyperlink. Consider http://www.datapigtechnologies.com/blog/index.php/emailing-from-excel-using-the-hyperlink-function/

    A hyperlink in this way would only fire when clicked.

    0 讨论(0)
  • 2020-11-30 13:55

    To run the function only on a click you can try entering as a subaddress:

    =HYPERLINK("#generateEmail(H2, I2, M2)", "Generate Email")
    

    and then add an extra line in the code to return the current address:

    Function generateEmail(name, manager, cc)
    
    Set generateEmail = Selection
    'Paste Outlook Code Here 
    
    End Function
    

    Note this method does not get executed on the main vba thread so to test you can insert statements in the code instead of stepping through, eg:

    Range("A10:C10") = Array(name, manager, cc)
    
    0 讨论(0)
  • 2020-11-30 13:56

    I ran into similar limitations on a project. I had to give up using hyperlink and created a workaround using the Worksheet_SelectionChange event. Assuming your formula was supposed to be on the N column, please consider using the following code inserted on your target sheet (not in a module):

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        If Target.Column = 14 Then
            Call generateEmail(Target.Offset(0, -6).Value2, Target.Offset(0, -5).Value2, Target.Offset(0, -1).Value2)
        End If
    
    End Sub
    

    Then you can add any text you want in that column (ie "Send Email') and when someone clicks on that cell your code will fire specifically with data from that row. Hope this helps. Regards.

    0 讨论(0)
  • 2020-11-30 14:08

    Using hyperlinks to trigger actions is a bit tricky:

    • HYPERLINK() links don't trigger Worksheet_FollowHyperlink event handlers
    • When using the "Insert Hyperlink"-type links you need a valid target address, and this can result in the selection jumping around when the user clicks on a link.

    Here's one approach - place this code in the worksheet module:

    Option Explicit
    
    Dim oldAdd As String
    
    'When a link is clicked, this gets triggered first
    '   if the clicked cell has a hyperlink, remember its address
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim c As Range
    
        'only want single-cell selections...
        If Target.Cells.CountLarge > 1 Then
            Exit Sub
        Else
            'cell has a link? Remember the address...
            If Target.Hyperlinks.Count > 0 Then
                oldAdd = Target.Address()
            End If
        End If
    End Sub
    
    'This is called immediately after the SelectionChange event,
    '  so we can use the stored address to reset the selection back
    '  to the clicked cell, instead of wherever its link was pointing to
    Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    
        Dim c As Range
    
        If oldAdd <> "" Then
            Set c = Me.Range(oldAdd)
            c.Select 'undo any navigation
            'Here you could switch on the link text if you have
            '   links which need to trigger different actions
            If c.Value = "Send Mail" Then
                MsgBox "Sending mail!"
                'generateEmail [use values from cells relative to c]
            End If
        End If
    
    End Sub
    

    Assumes all links point to the same sheet as the one the links are on.

    0 讨论(0)
提交回复
热议问题