2017-01-04 49 views
0

我已使用this黑客截取單元格上的鼠標移動。 在A1我已經估算公式如下:Excel中的公用函數無法正常工作,因爲UDF

=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 

爲什麼這種奇怪的行爲?

+0

即使你可以* *改變細胞與UDF格式,你實際上並沒有***返回* **任何東西,所以你對HYPERLINK的第一個參數是'Empty'。 – Comintern

+0

@Comintern - 檢查問題頂部的鏈接 –

回答

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 
+0

您可以設置顏色,但不設置線條樣式。我也想象寬度。所以,它也不適用於你。 – tic

+0

對不起,我打算重量,而不是寬度。 – tic