VBA to create dropdown list in a cell with formula = named range that is in another cell

ShoYnn

Board Regular
Joined
Mar 20, 2019
Messages
60
Office Version
  1. 2016
Platform
  1. Windows
Kind of a long title, but as it says I am trying to add a data validation list to cell D5 with the formula referencing a named range that is typed into cell C5. This is what I have

Range("D5").Select
With Selection.Validation
.Add Type:=xlValidatList, Formula1:=Range("C5").Value

And I keep getting Application-defined or object-defined error with that line highlighted. I have tried dimmimg rng as a string and having that be the C5 value then having the formula:=rng with the same results. How can I fix this?
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
How about ...
VBA Code:
Range("D5").Validation.Add Type:=xlValidateList, Formula1:="=indirect(c5)"
 
Upvote 0
Solution
How about ...
VBA Code:
Range("D5").Validation.Add Type:=xlValidateList, Formula1:="=indirect(c5)"
Lol, so that didn't work, I even just tried typing the indirect formula in the data validation myself and it said it calculated to an error, but I did fix it! Turns out I had my sheet lock itself earlier in the code and it didn't like doing that function as it would change the value on a locked sheet, which is silly since the cell itself is not locked! But anyway, it is fixed, tested, and working. I managed to get it to work with the code in my original post after ensuring it happens while my sheet is unlocked. Thanks for responding though!
 
Upvote 0
You are welcome and thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,727
Members
449,049
Latest member
MiguekHeka

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