Share via

Data Validation using if formula

Anonymous
2025-04-08T14:14:36+00:00

Hi All,

I'm trying to add a data validation to a range shows below code

ThisWorkbook.Names(TargetRangeName).RefersToRange.Validation.Add _

            Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= \_ 

                xlBetween, Formula1:="=IF(LEFT(M\_SenderCompanyCode,4)=""PK01"",ARGL8999,ARGLPAK)"

where

  1. TargetRangeName is a named range where we should add validation list

2.M_SenderCompanyCode is a named range of a single cell address

  1. A table 1 is their which one column is named as a range "ARGL8999"
  2. A table 2 is their which one column is named as a range "ARGLPAK"

the error pop up is

Run-time error '1004'

But when i add this formula(=IF(LEFT(M_SenderCompanyCode,4)="PK01",ARGL8999,ARGLPAK)) in excel data validation list it works but through code only it doesn't work.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2025-04-08T18:39:09+00:00

    Range names not used in formulas need to be in quotes:

    ThisWorkbook.Names("TargetRangeName").RefersToRange.Validation.Add _

    Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _

    xlBetween, Formula1:="=IF(LEFT(M_SenderCompanyCode,4)=""PK01"",ARGL8999,ARGLPAK)"

    Was this answer helpful?

    0 comments No comments