簡體   English   中英

Office 365 for Mac:在表更改時執行 VBA 宏

[英]Office 365 for Mac: Execute VBA macro on table change

我目前正在使用 Office 365 訂閱運行 Excel for Mac。 每當某個單元格值發生變化時,我都會嘗試執行 VBA 宏。 我已經在網上查看並看到了許多使用Worksheet_Change Sub 的代碼示例,但是它對我不起作用。 這是我的代碼目前的樣子:

Private Sub Worksheet_Change(ByVal Target as Range) 
    Target.Font.ColorIndex = 5 
End Sub

我正在嘗試在四張紙中的第二張上運行它。 我在第二張紙上單擊了View Code ,所以我在正確的工作表中。

有人知道為什么它有效/可能無效嗎? 它在 Excel for Mac 中不起作用嗎?

如果您需要更多信息,請告訴我。

謝謝

我之前也遇到過類似的問題,因為你的症狀聽起來像我的(代碼甚至沒有執行),我懷疑你的代碼實際上不在正確的工作表上。 Mac 中的 VBA 窗口比 Windows 中的詳細得多,我覺得它很混亂。

確保在左側窗格中單擊適當的下拉菜單

VBAProject(WorkbookName) -> Microsoft Excel Objects -> Sheet2

並在那里插入 Worksheet_Change 函數。

此外,如果您希望顏色僅在特定范圍內更改,則需要使用 Intersect:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range

    ' The variable KeyCells contains the cells that will cause an alert when they are changed.
    Set KeyCells = Range("A1:Z99")

    'This part executes only if the cell being changed is in KeyCells
    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then

        Target.Font.ColorIndex = 5 

    End If

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM