Problem with VBA worksheet Function - Runtime Error 1004

Derek Fingleson

New Member
Joined
Jan 22, 2020
Messages
16
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hi Guys

I have a simple worksheet.
Col A - Numbers from A1 to A56 ... Numbers 1 to 56
Col B - Rows B1 to B56 .. The RGB Color eg..RGB(255,0,255) .. Different rgb codes for all 56 rows
Col C - Blank
Col D - Rows 2, 3, 4 ,5 ,6 .. Simply have a different letter in each

What I'm trying to achieve is simple ..
1. Run the Macro
.. Each time you run it , it allocates a random number between 1 and 56
.. Vlookup then gets that number , finds it in Col A , Returns the value in Col B
... Then the Cell Colors in Col D2:D6 keep changing randomly when macro run


SO THIS IS WHAT I GOT IN MY VBA SO FAR .. But its not working:-

Please help a Bud out here !!

VBA Code:
Public Function RandNum()

RandNum = Application.WorksheetFunction.RandBetween(1, 56)


End Function


Sub ChangeBackgourdColorRGB_Range()
    Range("D2:D2").Interior.Color = Application.WorksheetFunction.VLookup("RandNum", "A2:B57", 2, False)
    Range("D3:D3").Interior.Color = Application.WorksheetFunction.VLookup("RandNum", "A2:B57", 2, False)
    Range("D4:D4").Interior.Color = Application.WorksheetFunction.VLookup("RandNum", "A2:B57", 2, False)
    Range("D5:D5").Interior.Color = Application.WorksheetFunction.VLookup("RandNum", "A2:B57", 2, False)
    Range("D6:D6").Interior.Color = Application.WorksheetFunction.VLookup("RandNum", "A2:B57", 2, False)
   
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
You're currently looking up the text "RandNum" but you should be looking up the result of the function. Change this line:

Code:
Application.WorksheetFunction.VLookup("RandNum", "A2:B57", 2, False)

to this:

Code:
Application.WorksheetFunction.VLookup(RandNum, "A2:B57", 2, False)

in each case.
 
Upvote 0
Hi Rory

Have tried that already , as well

Still getting " Run-time error '1004' Unable to get the Vlookup property of the WorksheetFunction class "
 
Upvote 0
Try

VBA Code:
Sub ChangeBackgourdColorRGB()

Range("D2:D2").Interior.Color = Application.WorksheetFunction.VLookup(Application.WorksheetFunction.RandBetween(1, 56), Range("A2:B57"), 2, False)
Range("D3:D3").Interior.Color = Application.WorksheetFunction.VLookup(Application.WorksheetFunction.RandBetween(1, 56), Range("A2:B57"), 2, False)
Range("D4:D4").Interior.Color = Application.WorksheetFunction.VLookup(Application.WorksheetFunction.RandBetween(1, 56), Range("A2:B57"), 2, False)
Range("D5:D5").Interior.Color = Application.WorksheetFunction.VLookup(Application.WorksheetFunction.RandBetween(1, 56), Range("A2:B57"), 2, False)
Range("D6:D6").Interior.Color = Application.WorksheetFunction.VLookup(Application.WorksheetFunction.RandBetween(1, 56), Range("A2:B57"), 2, False)

End Sub
 
Upvote 0
ColorIndexExcel VBA Color
1RGB(0,0,0)A
2RGB(255,255,255)B
3RGB(255,0,0)C
4RGB(0,255,0)D
5RGB(0,0,255)E
6RGB(255,255,0)
7RGB(255,0,255)
8RGB(0,255,255)
9RGB(128,0,0)
10RGB(0,128,0)
11RGB(0,0,128)
12RGB(128,128,0)
13RGB(128,0,128)
14RGB(0,128,128)
15RGB(192,192,192)
16RGB(128,128,128)
17RGB(153,153,255)
18RGB(153,51,102)
19RGB(255,255,204)
20RGB(204,255,255)
 
Upvote 0
Apologies - I misread your code and didn't see you were missing a range call too:

Rich (BB code):
Application.WorksheetFunction.VLookup(RandNum, Range("A2:B57"), 2, False)
 
Upvote 0
You cannot set the interior colour like that.
The values in col B are just strings, so you either need to split the string into it's individual elements, or just put a number in the cell instead.
With the sample date you have shown the RGB values are the same as the colorIndex values, so you could just use that instead.
 
Upvote 0
Because you have RGB in the cell it is not a valid color you are looking up RGB(0,0,250) not 0,0,250. Do you have to have the RGB in the cell?

This works with the code I posted
Book1
AB
21255,0,255
32254,0,255
Sheet4
 
Upvote 0

Forum statistics

Threads
1,214,851
Messages
6,121,931
Members
449,056
Latest member
denissimo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top