Adding Data Validation Via VBA Returns 1004 Error

Bdra

New Member
Joined
Jul 19, 2008
Messages
41
I thought this would be easy. I have set up 3 radio buttons on a worksheet. Corresponding to these are 3 named ranges on a separate sheet. As each radio button is selected, I am trying to make it change the data validation list in Range("D12") to the corresponding named range.

The range names are "Running" (for running expenses), "Bills", and "Debts".

Here is the code for the first radio button. The other two are identical, except for the range name:

Code:
Private Sub OptionButton1_Click()

    With Range("D12").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=Running"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With

End Sub
For some reason this breaks on the .Add line, with a run-time error 1004, "Application-defined or object-defined error."

The only suggestion I have been able to find, searching several forums, is that 1004 occurs when the name doesn't translate into a valid range. The ranges are defined dynamically, using the following formula (only column names differ between the three):

=OFFSET(Categories!$A$2,0,0,COUNTA(Categories!$A:$A)-1,1)

But I can add the validation lists manually without problems. I also tried substituting static named ranges and running the code, and it ran into the same error. So I am at a loss.

Any suggestions will be much appreciated.
 
Hi,

I am writing a function in excel vba to enforce users to input their gmail address. The following is the code. I always get a "run time error 1004' error message saying that "Application-defined or object-defined error". Please advise.


Sub CheckEmailAddress()
Range("I12").Activate
With ActiveCell.Validation
.Delete
.Add Type:=xlValidateCustom, Formula1:="=RIGHT(I12,9)="gmail.com"
'AlertStyle:=xlValidAlertStop, _
'.InputTitle = "Email Address"
'.ErrorTitle = "Error"
'.InputMessage = "Enter your email address (#####@gmail.com) to receive response."
'.ErrorMessage = "Please enter your gmail (e.g. #####@gmail.com)."
End With
End Sub
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi,

May be...

Code:
Range("d12").Activate
With ActiveCell.Validation
    .Delete
    .Add xlValidateList, Formula1:="=Running"
End With

Hi KrishnaKumar... I know this is a decade old post. But if you get a chance to see this... I just want to thank you. I was searching so long for a solution to this query and your solution... just worked..!! Cheers.
 
Upvote 0
Hi,

May be...

Code:
Range("d12").Activate
With ActiveCell.Validation
    .Delete
    .Add xlValidateList, Formula1:="=Running"
End With
Thank you so much, I've been trying for at leats 8 hours to solve this probleme. I create an account specificly to thank you 16 years after you posted the solution. Hope you see this and that your life as been gradualy better since 2008
 
Upvote 0

Forum statistics

Threads
1,215,606
Messages
6,125,800
Members
449,261
Latest member
Rachel812321

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