vba data validation with if statement

Status
Not open for further replies.

yichuansancun

Board Regular
Joined
Feb 7, 2011
Messages
123
i have the following codes:

Sub GSDDropDown()
If Range("AssignmentType").Value = Range("A1") Then
Range("EntrySub").Validation.Add Type:=xlValidateList, Formula1:="=GSD_Sub_DropDown"
Else
Range("EntrySub").Value = "N/A"
End If
End Sub


it is giving me Run-time Error 13 - Type mismatch. what did i do wrong? :confused:

P
 

Some videos you may like

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Jerry Sullivan

MrExcel MVP
Joined
Mar 18, 2010
Messages
8,787
I can't tell from your code why you are getting a Run-time Error 13.
You should add a .Delete statement otherwise you will get an Error 1004 if you try to run the code when the cell already has Data Validation.

Code:
Sub GSDDropDown()
 If Range("AssignmentType").Value = Range("A1") Then
    With Range("EntrySub").Validation
[COLOR="#0000CD"][B]        .Delete[/B][/COLOR]
        .Add Type:=xlValidateList, Formula1:="=GSD_Sub_DropDown"
    End With
 Else
    Range("EntrySub").Value = "N/A"
 End If
End Sub

When I mock up an example, this works fine.

Assuming that GSD_Sub_DropDown is a Name, what is listed in the Refers to: field for this Name in the Name Manager?

What line of code is the debugger highlighting when the error occurs?
 

Jerry Sullivan

MrExcel MVP
Joined
Mar 18, 2010
Messages
8,787
One other possibility...

You'll get a Run-time Error 13 - Type mismatch if either
Range("AssignmentType") or Range("A1") evaluate to errors.

Try this instead to handle that scenario:
Code:
Range("AssignmentType").Text= Range("A1").Text
 
Status
Not open for further replies.

Watch MrExcel Video

Forum statistics

Threads
1,127,205
Messages
5,623,364
Members
415,969
Latest member
Rey99

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
Top