难以在excel mac中将超链接链接到宏

时间:2016-12-28 16:04:57

标签: vba

我已经多次查看过这个问题但它似乎仍然不适合我。我正在学习VBA,作为一个练习项目我正在尝试制作营养跟踪器。我正在尝试制作一个超链接,当点击它时,运行一个宏来添加一个新的食物入口行。这是我的NewFoodEntry():

Sub NewFoodEntry()
'
' NewFoodEntry Macro
' Insert new food entry row.
'
' Keyboard Shortcut: Option+Cmd+Shift+N
'
' Setting selection as input cell'
    Dim myRange As Range
    Set myRange = Selection

' Select default food entry row'

    Range(myRange.Offset(-1, 0), myRange.Offset(-1, 7)).Select
    Selection.Copy

'Insert default food entry row below myRange selection'
    myRange.Insert Shift:=xlDown
    myRange.Select
    Application.CutCopyMode = False

'Select correct Totals cell'
    Dim totalsRange As Range
    Set totalsRange = Sheets("Nutrition").Range("D:D").Find("Totals", After:=myRange.Offset(0, 3), SearchOrder:=xlByRows, SearchDirection:=xlNext)
    Debug.Print totalsRange.Address

'Select correct Calories cell'

    Dim caloriesRange As Range
    Set caloriesRange = Sheets("Nutrition").Range("E:E").Find("Calories", After:=totalsRange.Offset(0, 1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious)

    If caloriesRange Is Nothing Then
        Debug.Print "Calories still not found."

    Else
        Debug.Print caloriesRange.Address

    End If

'Select correct Protein cell'

    Dim proteinRange As Range
    Set proteinRange = Sheets("Nutrition").Range("F:F").Find("Protein", After:=totalsRange.Offset(0, 2), SearchOrder:=xlByRows, SearchDirection:=xlPrevious)

    If proteinRange Is Nothing Then
        Debug.Print "Protein still not found."

    Else
        Debug.Print proteinRange.Address

    End If

'Select correct Carbs cell'

    Dim carbsRange As Range
    Set carbsRange = Sheets("Nutrition").Range("G:G").Find("Carbs", After:=totalsRange.Offset(0, 3), SearchOrder:=xlByRows, SearchDirection:=xlPrevious)

    If carbsRange Is Nothing Then
        Debug.Print "Carbs still not found."

    Else
        Debug.Print carbsRange.Address

    End If

'Select correct Fat cell'

    Dim fatRange As Range
    Set fatRange = Sheets("Nutrition").Range("H:H").Find("Fat", After:=totalsRange.Offset(0, 4), SearchOrder:=xlByRows, SearchDirection:=xlPrevious)

    If fatRange Is Nothing Then
        Debug.Print "Fat still not found."

    Else
        Debug.Print fatRange.Address

    End If

    'Calculate Calories Total

    totalsRange.Offset(0, 1).Formula = "=SUM(" & caloriesRange.Offset(1, 0).Address & ":" & totalsRange.Offset(-1, 1).Address & ")"

    'Calculate Protein Total

    totalsRange.Offset(0, 2).Formula = "=SUM(" & proteinRange.Offset(1, 0).Address & ":" & totalsRange.Offset(-1, 2).Address & ")"

    'Calculate Carbs Total

    totalsRange.Offset(0, 3).Formula = "=SUM(" & carbsRange.Offset(1, 0).Address & ":" & totalsRange.Offset(-1, 3).Address & ")"

    'Calculate Fat Total

    totalsRange.Offset(0, 4).Formula = "=SUM(" & fatRange.Offset(1, 0).Address & ":" & totalsRange.Offset(-1, 4).Address & ")"


End Sub

这是在"模块1"我的VBA编辑器。

现在我也在我的VBA编辑器中找到了正确的工作表对象("营养")并输入了代码。

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Target.Range.Address = "$A$37" Then 
        Debug.Print "It worked"
        Exit Sub
    End If

End Sub

我似乎无法使此代码正常工作。单击超链接不会打印到即时窗口。显然,通过超链接运行宏也不起作用。这是我的excel的截图,这样你就可以了解我想做什么。

Excel Screenshot

我还应该补充一点,这是我用来将单元格文本转换为超链接的代码:

Sub HyperActive()

'Make a hyperlink

    Dim nm As String

    nm = ActiveSheet.Name & "!"
    For Each r In Selection
        t = r.Text
        addy = nm & r.Address(0, 0)
        ActiveSheet.Hyperlinks.Add Anchor:=r, Address:="", SubAddress:= _
            addy, TextToDisplay:=r.Text
    Next r
End Sub

编辑: 经过很多挫折之后,我发现Worksheet_FollowHyperlink在Excel 2010之后无法在Mac上运行。我不知道为什么或者这是否真的如此,所以如果有人对此有所了解,请告诉我。

https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_mac/mac-excel-2011-hyperlinks-dont-work-as-buttons/41192768-45fd-47db-ad8c-f614fc83be5a

0 个答案:

没有答案
相关问题