Using a Variable with the INDIRECT function

Scott JS

New Member
Joined
Aug 3, 2005
Messages
35
Am trying to write a simple program to create a validation list in select cells in a spreadsheet. (Office 2010, Windows 7)

This code works:
ActiveCell.Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=indirect($C3)"


While the code above works, I do not want an absolute reference. The code below won’t work (Run Time Error 1004 – Application Defined or object defined error).

j = ActiveCell.Row
ActiveCell.Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=indirect($C & j & )"

All I am changing is
from: Formula1:="=indirect($C3)" <-- the code that works
to: Formula1:="=indirect($C & j & )"
or: Formula1:="=indirect($C” & j & ”)"

Am I missing something in my syntax or is this a limitation with the Indirect function?
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
This should work
Formula1: = "=INDIRECT($C" & j & ")"
provided there is a valid reference in the cell in column C on the same row.

M.
 
Upvote 0
This should work
Formula1: = "=INDIRECT($C" & j & ")"
provided there is a valid reference in the cell in column C on the same row.

M.
Marcelo -- Thanks, Appreciate the suggestion. What you have there is actually one of the options I had tried/referenced in my post. Am beginning to think this is an Indirect function issue. Have never been able to get variables in VBA to work with it.
 
Upvote 0
Marcelo -- Thanks, Appreciate the suggestion. What you have there is actually one of the options I had tried/referenced in my post. Am beginning to think this is an Indirect function issue. Have never been able to get variables in VBA to work with it.

It worked for me. For example:
put in C4
A1:A3

in A1:A3
blah
bleh
blih

Then with cursor in D4 (Active cell) run this macro

Code:
Sub aTest()
    Dim j As Long
    
    j = ActiveCell.Row
    ActiveCell.Select
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=indirect($C" & j & ")"
    End With
End Sub

M.
 
Upvote 0
Another way

Name the range A1:A3 as MyRange

Put in C4
MyRange

With cursor in D4 (ActiveCell) run the macro above

M.
 
Upvote 0
If the line
xlBetween, Formula1:="=indirect($C3)"

works in your code then the line
xlBetween, Formula1:="=indirect($C" & j & ")"

should work as well.

One thing I can say is that the quotes as shown in your post are not the standard type of quotation marks used in VBA. If the quotes in your VBA code are slanted like shown, delete them and retype them.
($C" & j & ")" not ($C & j & )"
 
Upvote 0
One thing I can say is that the quotes as shown in your post are not the standard type of quotation marks used in VBA. If the quotes in your VBA code are slanted like shown, delete them and retype them.
($C" & j & ")" not ($C & j & )"

Good eyes!! I hadn't noticed this.

M.
 
Upvote 0
Good eyes!! I hadn't noticed this.

M.

Y'all, thanks for the great counsel. It works. needed to populate the referenced cell -- when it is populated, the code works, when it is blank, the code does not. Weird considering I can create and copy the validation list manually when the referenced column is blank.

Thanks for your insights.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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