VBA Macro: Application-defined or object-defined error - vba

I'm struggling with the syntax on a VBA macro. Trying to create a select from Sheet 2 list on Sheet 1, and it gives me Run-time error '1004': Application-defined or object-defined error.
Sub Macro1()
Sheets("Sheet1").Select
Sheets("Sheet1").Range("G3").Select
With Selection.Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="Sheet2!$A$1:$A$14"
.IgnoreBlank = True
.InCellDropdown = True
End With
End Sub
Please help me understand why I am getting this error and how to fix it?

If you are running the macro more than once, you need to ensure you delete any existing validation
Sub Macro1()
With Sheets("Sheet1").Range("G3").Validation
'Remove existing validation
.Delete
'Add new validation
.Add Type:=xlValidateList, _
AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, _
Formula1:="=Sheet2!$A$1:$A$14"
'Note - need ^ (i.e. the equals sign to make it a formula)
.IgnoreBlank = True
.InCellDropdown = True
End With
End Sub

Related

Data Validation List Error

I'm trying to add a data validation pulldown list, and I've been debugging it for a while to no avail. I'm getting a Run-time error 1004, Application-defined or object defined error. The error occurs in Formula1 setting part of the Validation.Add statement.
I've tried using a string reference to a named range, a string reference to a standard range, and, as shown below, a comma delimited list string generated from the list on the worksheet as shown in the code below. I have checked the list string with Debug.Print and got the expected result.
Sub addPT_Validation()
Dim sValidationList As String
Dim cell As Range
For Each cell In ThisWorkbook.Names("PT_Puldown").RefersToRange
sValidationList = sValidationList & cell.Value & ","
Next cell
sValidationList = Left(sValidationList, Len(sValidationList) - 1)
With ActiveSheet.Range("D14").Validation
.Add Type:=xlValidateList, _
AlertStyle:=xlValidAlertStop, _
Operator:=xlEqual, _
Formula1:=sValidationList
.IgnoreBlank = True
.InCellDropdown = True
.ShowError = True
End With
End Sub
Thanks in advance for any guidance.
All your code is working (certainly could be optimized), all you need to do is delete the validation if it already exists.
If you try to add validation to a cell which already contains one, you will get error 1004.
Sub addPT_Validation()
Dim sValidationList As String
Dim cell As Range
For Each cell In ThisWorkbook.Names("PT_Puldown").RefersToRange
sValidationList = sValidationList & cell.Value & ","
Next cell
sValidationList = Left(sValidationList, Len(sValidationList) - 1)
With ActiveSheet.Range("D1").Validation
'/Delete first., in case of any any existing validation
.Delete
.Add Type:=xlValidateList, _
AlertStyle:=xlValidAlertStop, _
Operator:=xlEqual, _
Formula1:=sValidationList
.IgnoreBlank = True
.InCellDropdown = True
.ShowError = True
End With
End Sub
I'd use the named range reference to build up the formula to put in Formula1 parameter of Validation object Add() method, like follows
Sub addPT_Validation()
Dim formula As String
With ThisWorkbook.Names("PT_Puldown").RefersToRange
formula = "'" & .Parent.Name & "'!" & .Address(External:=False)
End With
With ActiveSheet.Range("D14").Validation
.Add Type:=xlValidateList, _
AlertStyle:=xlValidAlertStop, _
Operator:=xlEqual, _
Formula1:=formula
.IgnoreBlank = True
.InCellDropdown = True
.ShowError = True
End With
End Sub
As pointed out by #cyboashu, your error is coming from the fact that you did not delete the validation before adding a new one.
With regard to your other issue of trying to pass the range to Formula1 directly:
Formula1 should refer to the range as a string.
eg:
Formula1:="=$G$2:$G$7"
or, using your variables:
Formula1:="=" & ThisWorkbook.Names("PT_Puldown").RefersToRange.Address
or, simpler:
Formula1:="=" & Range("PT_Puldown").Address

Dynamic Dropdrown VBA

I want to create an excel drop down in vba.
I am storing the values which I want in drop down in a named range, I want to use the name of the range in the code while creating drop down like below
Sheet1.Range("O3:O28").Name = "test"
With Range("rng_expensecategory").Offset(2, 0).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=& test"
.IgnoreBlank = True
.InCellDropdown = True
.ShowInput = True
.ShowError = True
End With
But the line xlBetween, Formula1:="=& test" is giving me an error.
Is there a turnaround or some other way which i can use to refer to the name range in the above code

VBA to programmatically put value in cell when adding data validation list

I am fully versed in VBA but not certain if this is possible or not as I couldn't find anything while searching for the past 15 minutes.
I add a data validation list in the usual manner
startCell.Offset(1, 2).Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=Categories"
.IgnoreBlank = True
.InCellDropdown = True
End With
But when the routine finishes none of the values are selected and therefore the cell is blank.
Is there a way to set the cell value when adding the data validation, i.e. a default value maybe the first item in the data validation list?
Try this:
With startCell.Offset(1, 2)
With .Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:= xlBetween, Formula1:="=Categories"
.IgnoreBlank = True
.InCellDropdown = True
End With
.Value = Range("Categories").Cells(1).Value
End With

Dynamic validation list - data from other worksheet

I have one worksheet where in a specified range should be validation list. This list is token from another worksheet (column A, from row 2 to last not-empty row). But it doesn't work and I can't figured why.
My code:
Set wsResourcesProjects = Sheets("ResourcesProjects")
Set wsProjects = Sheets("Projects")
With wsProjects.rGeneralFTERange.Offset(0, 3).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=ResourcesProjects!" & wsResourcesProjects.Cells(1, 1).CurrentRegion.Offset(1, 0).Address
.IgnoreBlank = True
.InCellDropdown = True
.ShowInput = True
.ShowError = True
End With
And in range rGeneralFTERange validation list is not appear. But when I change this part:
wsResourcesProjects.Cells(1, 1).CurrentRegion.Offset(1, 0).Address
on
"A2:A10"
then it's works. But it's not good for me, because data in column "A" are dynamic.
In your code Formula1:="=ResourcesProjects!" exclimation ! symbol might have created the problem, hope you have already created the named ranges :)
FYR named ranges
Sub validation()
'Select your range
Range("A1").Select
With Selection.validation
.Delete
'Month_Val is the namedrange name
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=Month_Val"
End With
End Sub

Run-time error 1004 when adding data validation. How do I avoid the error?

I am using the following procedure to add data validation to a cell:
Sub SetDataValidationList(r As Range, s As String)
Dim dv As Validation
Set dv = r.Validation
With dv
.Delete
If s <> "" Then
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=s
Else
'The backup string could be a parameter
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="None available"
End If
End With
End Sub
It worked while I was developing the Excel file for a few days, but now for some reason the row .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=s throws run-time error 1004. What might be the reason? How can I avoid the error?
s is "1.1,PM,PA,OE,MOA1,MOA2,MOA3,MOA4"
I just figured it out. The reason is that the sheet was protected.