Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9192899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:02:30+00:00 2026-06-17T21:02:30+00:00

I am trying to create data validation cells purely with VBA. The problem is,

  • 0

I am trying to create data validation cells purely with VBA. The problem is, if one of the
items in the validated list is a Excel formula which contains commas (such as the IF() in the following code) an error is generated.

 Dim str As String
 str = "=IF(SUM(A1:A2) = 0, ""Zero"", SUM(A1:A2)) , Item 2 , Item 3 , Item 4"

 s1.Range("B6").Validation.Add Type:=xlValidateList, _
          AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=str

The error (runtime error 1004) is generated because it delimits the string at the first comma, so the first item of the validation is "=IF(SUM(A1:A2) = 0". I’m hoping there are escape characters for the commas (such as \ in C) to avoid this.
I want the validated list to appear as such:

<result of IF() function>
Item 2
Item 3
Item 4

Note: I CANNOT use a range for the validated data. If the user deleted the range the spreadsheet would be broken.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:02:31+00:00Added an answer on June 17, 2026 at 9:02 pm

    How about a WorkSheet_Change event to reset the data validation when cells A1 or A2 change?

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim ValidationString As String
    Dim rngChanging As Excel.Range
    Dim rngWithValidation As Excel.Range
    
    Set rngChanging = Range("A1:A2")
    Set rngWithValidation = Range("B6")
    If Not Intersect(Target, rngChanging) Is Nothing Then
        With rngWithValidation
            .Validation.Delete
            If WorksheetFunction.Sum(rngChanging) = 0 Then
                ValidationString = "0"
            Else
                ValidationString = WorksheetFunction.Sum(rngChanging)
            End If
            ValidationString = ValidationString & ",Item2,Item3,Item4"
            .Validation.Add Type:=xlValidateList, Formula1:=ValidationString
        End With
    End If
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are trying to create an excel document which supports macros with some data
I am trying to create a data validation list whose source comes from a
I am trying to create a list where I want users to enter data
We are trying to create some custom validation with MVC 4 data annotations, the
I'm trying to create a data extractor algoritm from group buying sites to build
I'm trying to create core data application. Some times when trying to save data,
I'm trying to create my first data mart. I am creating my dimension table
The task: I am trying to create a custom data type and have it
I am trying to create a report that pulls data from 2 tables: a
I am trying to create some charts of data (eg http://www.amibroker.com/ ). Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.