vba 在自定义函数中怎么改变单元格背景色?很特别!

2025-04-23 16:59:50
推荐回答(2个)
回答1:

函数没错,但是函数不能用来执行把背景变为红色等过程,函数返回的只有值.
如果你要执行过程,需要另外编一个过程.不能放到这个函数里

回答2:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim lim As Integer
Dim bodyLen As Integer
Dim cell As Range

For Each cell In Range("1:1")
lim = Len(cell)
bodyLen = Len(cell.Offset(1, 0))

If bodyLen > lim Then
cell.Interior.ColorIndex = 36
Else
cell.Interior.ColorIndex = xlNone
End If
Next
End Sub

在工作表里面添加Selectiongchange事件