ClimoC
Well-known Member
- Joined
- Aug 21, 2009
- Messages
- 584
Hi guys
This works fine as a macro, but when I tried sticking it in a module, saving it as an add-in, and using it in the worksheet, it fails with a non-descript 1004 error.
It's for Excel 2007-> , and is to colour cells on a scale (Yes, I am aware of colour-scaling conditional formatting, but I need the actual Cell.Interior to change, not conditional formatting, for other scripts). The parameters are the cell value (a double), and it's relation to the average of a range of numbers (2nd parameter)
As I said, works fine as a standalone function. As a UDF in an addin to be used as a worksheet function, it fails.
Any ideas?
Thanks
C
This works fine as a macro, but when I tried sticking it in a module, saving it as an add-in, and using it in the worksheet, it fails with a non-descript 1004 error.
Code:
Public Function HexColour(ByRef Rng As Range, Ar As Range)
Dim myScale As Double
Dim Av As Double, Mi As Double, Ma As Double
Dim Rd As Integer, Gr As Integer, Bl As Integer
Application.Calculation = xlCalculationManual
Av = Application.WorksheetFunction.Average(Ar)
Mi = Application.WorksheetFunction.Min(Ar)
Ma = Application.WorksheetFunction.Max(Ar)
Debug.Print Application.WorksheetFunction.Average(Ar)
Debug.Print Application.WorksheetFunction.Min(Ar)
Debug.Print Application.WorksheetFunction.Max(Ar)
myScale = (255 / Ma)
Err.Clear
On Error Resume Next
Debug.Print RGB(Round(Rng.Value * myScale, 0), 0, Round(255 - Round(Rng.Value * myScale), 0))
Debug.Print VBA.Round(Rng.Value * myScale, 0)
Debug.Print VBA.Round(255 - (Rng.Value * myScale), 0)
Rd = CInt(VBA.Round(Rng.Value * myScale, 0))
Gr = 0
Bl = VBA.Round(255 - (Rng.Value * myScale), 0)
Rng.Interior.Color = RGB(Rd, Gr, Bl)
If Round(Rng.Value * myScale) < 100 Then Rng.Font.ColorIndex = 2 Else Rng.Font.ColorIndex = 1
Application.Calculation = xlCalculationAutomatic
MsgBox Err.Number
End Function
It's for Excel 2007-> , and is to colour cells on a scale (Yes, I am aware of colour-scaling conditional formatting, but I need the actual Cell.Interior to change, not conditional formatting, for other scripts). The parameters are the cell value (a double), and it's relation to the average of a range of numbers (2nd parameter)
As I said, works fine as a standalone function. As a UDF in an addin to be used as a worksheet function, it fails.
Any ideas?
Thanks
C