xls2003: data validation using variables in VBA

Vubb

New Member
Joined
Mar 30, 2009
Messages
4
Hi guys,

got a problem here and trying to solve it but the all things I come up with do not work :(

So, I have one range to fill with lists (data validations) dependent from another range. The key point is how to write the variables into the formula, red marker. If I just write in a hard cell like C10 the code works, but of course that's not what I need.

I would highly appreciate your help!

Here's part of the code:
a = 0
b = 16 + cAmount
For Each cell In wRange
If a > 12 Then
a = 1
Else
a = a + 1
End If
col = getColString(a) 'returns the needed column as a string
cell.Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=INDIRECT(" & col & row & ")"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Next cell
 

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.
Code:
Formula1:="=INDIRECT(""" & col & row & """)"
should work.

 
Upvote 0
Hi,

thanks a lot for the fast answer.

However, another problem occured before I could even test your solution.

Having the line
cell.Select
I get the error Select method of range class failed.

If I realign the two lines
cell.Select
With Selection.Validation

to

With cell.Validation
it runs into the -With- part. Unfortunately there comes the next error in line
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="randominput (tried different solutions, obviously is independent from this one"

error displayed: Method Add of object Validation failed.

Do you know the origin of this problem?
I think this problem even occured once and just starting the macro again would solve it... but next time it occured again, but I can't give definite info on this: I might have changed something so it would run, but I don't remember having changed anything.
 
Upvote 0
Is your worksheet protected?
 
Upvote 0
Then I would guess that your validation formula is not producing a valid range reference.
 
Upvote 0
okok, I solved the other problems:
1. I had to distinctly Activate the Sheet.
2. I did not notice that obviously in some versions of my Excel-Work the Reference-List was not named. Had to redo this.
3. Removed the Selection part and replaced it with cell.Validation
4. and most stupid: I forgot that the variable "row" did not even exist but was an "a" in my code.

-> With these changes the code in the first post did work ;)

Thanks a lot anyway. Man, I looked hours for the fault, now I'm getting myself a beer :D
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

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