我想在 Excel 中撰寫一個模塊或子,以便根據另一個單元格的值向單元格的注釋部分添加注釋。例如:F2 Cell的值為“High Level”,通過B2 Cell=GetComment(F2)的注釋部分等功能更改為“High Level”。
如果F2 的值為空,則應在B2 單元格的注釋部分添加“Nothing” 。
以下代碼不起作用,我只是試圖說明我的意思
Function GetComment(ByVal target As Range, rng As Range)
If IsEmpty(rng.Value) Then
target.AddComment ("Nothing")
Else
target.AddComment (rng.Value)
End If
End Function
為了更好地解釋,我捕獲了您可以在下面的鏈接中看到的 excel 環境: https ://s22.picofile.com/file/8448590268/IMG_20220324_WA0000.jpg
如果有人幫助我,我將不勝感激。提前致謝。
uj5u.com熱心網友回復:
如果您不使用 UDF,那么您需要像這樣更改您的代碼
Option Explicit
Function GetComment(ByVal target As Range, rng As Range)
If IsEmpty(rng.Value) Then
myCmt target, "Nothing"
'target.AddComment ("Nothing") <= This code will fail if a comment already exists
Else
myCmt target, rng.Value
'target.AddComment (rng.Value) <= This code will fail if a comment already exists
End If
End Function
Function myCmt(rg As Range, cmt As String)
' This will add the string cmt as a comment to the target
' and will delete any existing comment regardless
' Just delete any existing comment
If Not rg.Comment Is Nothing Then
rg.Comment.Delete
End If
rg.AddComment (cmt)
End Function
Sub TestIt()
GetComment [B2], [F2]
End Sub
在這篇文章中,您可以找到在這種情況下如何使用 UDF 的解決方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/457675.html
上一篇:如何在圖表中包含空單元格?
