Excel中的公共函数无法正常用作UDF

时间:2017-01-04 20:02:54

标签: excel excel-vba mouseover udf vba

我使用this hack拦截单元格上的鼠标移动。 在A1中,我推测了以下公式:

=IFERROR(HYPERLINK(OnMouseOver(ROW(),COLUMN()),"Click here"), "Click here")

然后我编写了以下函数来清除大范围内的所有边框,并在鼠标悬停的单元格周围绘制粗边框。

Public Function OnMouseOver(row, col)
Dim ra As Range
    Set ra = Sheets("Sheet1").Columns("A:BJ") 'Range("griglia")
    ra.Borders(xlDiagonalDown).LineStyle = xlNone
    ra.Borders(xlDiagonalUp).LineStyle = xlNone
    ra.Borders(xlEdgeLeft).LineStyle = xlNone
    ra.Borders(xlEdgeTop).LineStyle = xlNone
    ra.Borders(xlEdgeBottom).LineStyle = xlNone
    ra.Borders(xlEdgeRight).LineStyle = xlNone
    ra.Borders(xlInsideVertical).LineStyle = xlNone
    ra.Borders(xlInsideHorizontal).LineStyle = xlNone

    Set ra = ActiveSheet.Cells(row, col)
    ra.Borders(xlDiagonalDown).LineStyle = xlNone
    ra.Borders(xlDiagonalUp).LineStyle = xlNone
    With ra.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Color = -16776961
        .TintAndShade = 0
        .Weight = xlThick
    End With
    With ra.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Color = -16776961
        .TintAndShade = 0
        .Weight = xlThick
    End With
    With ra.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Color = -16776961
        .TintAndShade = 0
        .Weight = xlThick
    End With
    With ra.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Color = -16776961
        .TintAndShade = 0
        .Weight = xlThick
    End With
End Function

此功能不会清除现有边框,而且仅在A1周围绘制细边框。另一方面,当我通过这段代码运行它时,它按预期工作:

Sub testfunction()
Call OnMouseOver(1, 1)
End Sub

为什么会出现这种奇怪的行为?

1 个答案:

答案 0 :(得分:0)

这对我有用 - 我可以设置颜色但不能将线条样式设置为xlNone

Public Function OnMouseOver(row, col)
     With ActiveSheet
        .Range("C5:H23").Borders.Color = RGB(200, 200, 200)
        .Cells(row, col).Borders.Color = vbRed
    End With
End Function