Creating data validation using VBA

svjensen

Board Regular
Joined
Jun 10, 2009
Messages
118
I need to update a large number of workbooks, and do this using VBA.

One of the updates is to add data validation to a given cell.
I have recorded a macro, which I am then trying to use, but I keep getting a 'Runtime error 1004 Application-defined or object-defined error'.

The recorded macro is as follows (I am using a Danish version of Excel, so the formula is with Danish functions):
Code:
   With Selection.Validation
       .Delete
       .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
       xlBetween, Formula1:= _
       "=FORSKYDNING(INDIREKTE(VOPSLAG($D7;hovedmaterielmatrix;2;FALSK));0;0;TÆL.HVIS(INDIREKTE(VOPSLAG($D7;hovedmaterielmatrix;2;FALSK)&""Col"");""?*"")-2;1)"
       .IgnoreBlank = True
       .InCellDropdown = True
       .InputTitle = ""
       .ErrorTitle = ""
       .InputMessage = ""
       .ErrorMessage = ""
       .ShowInput = True
       .ShowError = True
   End With

I suspect that the problem is the Danish functions, since I assume that VBA needs the English versions(?)

I have, therefore, tried replacing the formula with:
"=OFFSET(INDIRECT(HLOOKUP($D7;hovedmaterielmatrix;2;FALSE));0;0;COUNTIF(INDIRECT(HLOOKUP($D7;hovedmaterielmatrix;2;FALSE)&""Col"");""?*"")-2;1)"

But with the same result.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
The formula works fine when I insert it manually, but fails, as mentioned, when I try doing it with VBA.
 
Upvote 0
When I inserted it manually Excel told me that it currently evaluated to an error. Do you get that?

It's hard to test without knowing the layout of your worksheet and what hovedmaterielmatrix refers to.
 
Upvote 0
Ok.
I have a matrix of lists which I need to use for dependent list in my data validation.
This is created along the lines of http://www.contextures.com/xlDataVal02.html#Illegal and http://www.contextures.com/xlDataVal02.html#Dynamic

The parent list is displayed in cell D7, and based on that choice the list in the dependant list is defined (using the formula in question).
It evaluates to an error, when no seelction has been made in the parent list.

I will se if I can upload the file somehow.
 
Upvote 0
Ok.
There is an example on the following link.

http://vinther-jensen.dk/excel/Example, dependent lists.xls

The sheet 'PP_Globaledata' contains the data lists with the NamedRanges which are used in the data validation formulas.

The sheet '1c' contains the dependent data validation lists.
Cells D7 and D8 have been set up manually to show you the functionality.

In cells D16 and D17 the second data validation needs to by added using VBA.

I hope this helps.

/Soren
 
Upvote 0
Soren

I've downloaded the file and I'm looking at it now.

One question though, where are you actually trying to put this data validation?

You've used Selection rather than specifying a particular range.

I'm not saying that's going to solve the problem but it's usually a good idea to be specific about that sort of thing.:)
 
Upvote 0
Actually I think I might have found the problem, and you might want to kick yourself.

It appears to be a simple typo, you have COUTIF instead of COUNTIF.

When I change that the code definitely works, though I couldn't say if it does what you want.:)
 
Upvote 0
It worked for me when I corrected for a typo:

Rich (BB code):
        "=OFFSET(INDIRECT(HLOOKUP($D7,hovedmaterielmatrix,2,FALSE)),0,0,COUNTIF(INDIRECT(HLOOKUP($D7,hovedmaterielmatrix,2,FALSE)&""Col""),""?*"")-2,1)"
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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