+ Reply to Thread
Results 1 to 5 of 5

VBA Excel adding dynamic validation list to multiple cells

Hybrid View

  1. #1
    Registered User
    Join Date
    12-12-2016
    Location
    Mumbai, India
    MS-Off Ver
    2013
    Posts
    2

    VBA Excel adding dynamic validation list to multiple cells

    I have created two drop down list of country and city which are dependent using this:

    https://siddharthrout.wordpress.com/...ent-lists-vba/

    However, now the issue is that I would like to create similar drop-downs below it using the same code in the file.

    So I replaced Range("B2") with Range("B2:B11") and Range("C2") with Range("C2:C11") everywhere in the code.

    But this is now giving the error 'Type Mismatch' and therefore not generating the required output. I am not good in vba so I am not able to understand what else should I change. Any help is greatly appreciated!

    Also, for country Australia in cell B2 in sheet 1, it is showing Sydney and NSW separately which I want to be together.

    Thanks in advance!
    Attached Files Attached Files
    Last edited by Lokesh3_14; 12-22-2016 at 07:08 AM.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: VBA Excel adding dynamic validation list to multiple cells

    This is one of my favorite techniques for cascading drop-down lists. Check it out to see if it will work for you.
    http://www.utteraccess.com/wiki/inde...ists_%28VBA%29
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA Excel adding dynamic validation list to multiple cells

    Hi Lokesh3_14

    Welcome to the Forum!!!

    I didn't rewrite your existing Code, merely modified it. As dflak noted, there are other ways to do this...some better than others. Modified Code..
    Private Sub Worksheet_Change(ByVal Target As Range)
    
       Dim i As Long, LastRow As Long, n As Long
       Dim LR           As Long
       Dim MyCol        As Collection
       Dim SearchString As String, Templist As String
    
       Application.EnableEvents = False
    
       LR = Range("A" & Rows.Count).End(xlUp).Row
    
    
       On Error GoTo Whoa
    
       ' Find LastRow in Col A
       LastRow = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
    
       If Not Intersect(Target, Columns(1).EntireColumn) Is Nothing Then
          Set MyCol = New Collection
    
          ' Get the data from Col A into a collection
          For i = 4 To LastRow
             If Len(Trim(Sheet2.Range("A" & i).Value)) <> 0 Then
                On Error Resume Next
                MyCol.Add CStr(Sheet2.Range("A" & i).Value), CStr(Sheet2.Range("A" & i).Value)
                On Error GoTo 0
             End If
          Next i
    
          ' Create a list for the Data Validation List
          For n = 1 To MyCol.Count
             Templist = Templist & "," & MyCol(n)
          Next
    
          Templist = Mid(Templist, 2)
    
          Range("B2:B" & LR).ClearContents: Range("B2:B" & LR).Validation.Delete
    
          ' Create the Data Validation List
          If Len(Trim(Templist)) <> 0 Then
             With Range("B2:B" & LR).Validation
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Templist
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .ShowInput = True
                .ShowError = True
             End With
          End If
    
          ' Capturing change in cell A2
       ElseIf Not Intersect(Target, Range("B2:B" & LR)) Is Nothing Then
          SearchString = Target.Value
          'SearchString = Range("B2").Value
    
          Templist = FindRange(Sheet2.Range("A2:A" & LastRow), SearchString)
    
          'Range("C2:C" & LastRow).ClearContents
          ' Range("C2:C" & LastRow).Validation.Delete
          Cells(Target.Row, "C").Validation.Delete
    
          If Len(Trim(Templist)) <> 0 Then
             ' Create the DV List
             With Cells(Target.Row, "C").Validation
                '    With Range("C2:C" & LastRow).Validation
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Templist
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .ShowInput = True
                .ShowError = True
             End With
          End If
       End If
    LetsContinue:
       Application.EnableEvents = True
       Exit Sub
    Whoa:
       MsgBox Err.Description
       Resume LetsContinue
    End Sub
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  4. #4
    Registered User
    Join Date
    12-12-2016
    Location
    Mumbai, India
    MS-Off Ver
    2013
    Posts
    2

    Re: VBA Excel adding dynamic validation list to multiple cells

    Hi John,

    Thanks very much for your help! That worked like a charm for me. Sure I will mark my Thread as SOLVED.

    Kind Regards,
    Lokesh

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA Excel adding dynamic validation list to multiple cells

    You're welcome...glad I could help.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to add Multiple Cells from Data Validation List
    By trumpetman in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 11-24-2014, 11:29 PM
  2. Adding Date Data Validation to cells with List Data Validation
    By biggtyme in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-03-2013, 09:47 AM
  3. Replies: 0
    Last Post: 04-15-2013, 09:22 AM
  4. Adding multiple cells based on the result of a data validation cell
    By ronrona96 in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 01-20-2013, 12:19 AM
  5. Dynamic dropdown list via data validation in Excel
    By ronin21 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-22-2012, 07:01 AM
  6. Replies: 0
    Last Post: 02-13-2012, 07:18 AM
  7. Replies: 0
    Last Post: 10-31-2007, 05:20 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1