Add data validation to table column via VBA

juanbolas

New Member
Joined
Dec 3, 2014
Messages
40
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm stumped. I'm trying to add validation to a particular column in a table and am getting a runtime error 1004 Application-defined or object-defined error in the '.ADD Type:= ...' line.

Any help is greatly appreciated. Code follows.

Thanks in advance

VBA Code:
Sub ABS_Validation()

    Dim LR As Long
    TargetWS = "DB_ABS"
    TargetColumn = "ABS_Data[[#Headers],[N° de Orden]]"
    FR = Sheets(TargetWS).Range(TargetColumn).Row + 1
    LR = Sheets(TargetWS).Range(TargetColumn).End(xlDown).Row
    ValidationRange = "U" & FR & ":U" & LR
    Debug.Print ValidationRange

    Set ws = ActiveWorkbook.Sheets(TargetWS)
    Set ApprovalsValidation = ws.Range(ValidationRange)
    
    ValidationList = Chr(160) & ",Aprobado,Rechazado"
    
    With ApprovalsValidation.Validation
        .Delete 'delete previous validation
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Formula1:=ValidationList
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = False
        .ShowError = True
    End With

End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Your named range should be in quotes.

VBA Code:
 Set ApprovalsValidation = ws.Range("ValidationRange")
 
Upvote 0
Hello @davesexcel. I tried it and it gives me an error on the line where I add quotation marks. ValidationRange is defined near the beginning of the code
 
Upvote 0
As ValidationRange is a variable it should not be in quotes.

Is the "DB_ABS" sheet protected?
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,984
Members
449,058
Latest member
oculus

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