VBA Code to Extract Data from a List and Making a Validation

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Hello and thanks in advance for your assistance. I would like to make a data validation on a particular sheet by extracting entries from a column on another sheet. I would also like to eliminate any duplicate values from the list.

My issue is how to get the values to appear in the formula bar. I would like to not use name ranges. I am having issues with the following line of code.

VBA Code:
    Sheets("Plan.Loader").Range("A7").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=RngDatVal

So basically is there some way for it to automatically extract and input the values into the formula to get this:
VBA Code:
    Sheets("Plan.Loader").Range("A7").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="Dog,Cat,Bird,Mouse"


1646156502729.png




The code is as follows. Also, how much does the code change if it needs to eliminate any duplicates values from the range. In terms of ensuring that none of the values repeat themselves in the data validation?

VBA Code:
Sub DatVal()

'Dimensioning
   Dim ShtNm As String
   Dim LastRow As Long
   Dim RngDatVal As Range
  
  
Sheets("Plan.Loader").Activate

'Code
    ShtNm = "Plan.2022.02.28"

    With Sheets(ShtNm)
       Set RngDatVal = .Range("C9:C17")
    End With

    Sheets("Plan.Loader").Range("A7").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Formula1:=RngDatVal


End Sub
 
Last edited by a moderator:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try:
VBA Code:
Sub DataVal()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, dic As Object
    v = Sheets("Plan.2022.02.28").Range("C9:C17").Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v) To UBound(v)
        If v(i, 1) <> "" Then
            If Not dic.exists(v(i, 1)) Then
                dic.Add v(i, 1), Nothing
            End If
        End If
    Next i
    With Sheets("Plan.Loader").Range("A7").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=Join(dic.Keys, ",")
    End With
    Application.ScreenUpdating = True
End Sub
Change the range to suit your needs.
 
Upvote 0
Solution
Is the range of values only C9:C17?
Thanks so much @Fluff for reviewing my code and responding so quickly with a question. Your help is always welcome.

It varies, but I was going to find the last row and just make the change.
 
Upvote 0
How many values could you possibly have, as there is a limit to the size of text string allowed?
 
Upvote 0
Try:
VBA Code:
Sub DataVal()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, dic As Object
    v = Sheets("Plan.2022.02.28").Range("C9:C17").Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v) To UBound(v)
        If v(i, 1) <> "" Then
            If Not dic.exists(v(i, 1)) Then
                dic.Add v(i, 1), Nothing
            End If
        End If
    Next i
    With Sheets("Plan.Loader").Range("A7").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=Join(dic.Keys, ",")
    End With
    Application.ScreenUpdating = True
End Sub
Change the range to suit your needs.

Thanks @mumps for your quick response and the solution. This works. I just need to dissect it a bit as the code is far more advanced than my VBA abilities. It was pretty slick on how you coded it.
 
Upvote 0
How many values could you possibly have, as there is a limit to the size of text string allowed?
Hi @Fluff. Not sure, but for now I don't think I need any more than 10, but this number could change in the future. I just don't think it will be anything over 15 or 20.
 
Upvote 0
In that case you also use this, which will ignore blank cells
VBA Code:
Sub OilEconomist()
   Dim Dic As Object
   Dim Cl As Range
  
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Plan.2022.02.28")
      For Each Cl In .Range("C9", .Range("C" & Rows.count).End(xlUp))
         If Cl.Value <> "" Then
            Dic(Cl.Value) = Empty
         End If
      Next Cl
   End With

   With Sheets("Plan.Loader").Range("A7").Validation
      .Delete
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=Join(Dic.Keys, ",")
   End With
End Sub
 
Last edited:
Upvote 0
In that case you also use this, which will ignore blank cells
VBA Code:
Sub OilEconomist()
   Dim Dic As Object
   Dim Cl As Range
 
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Plan.2022.02.28")
      For Each Cl In .Range("C9", Range("C" & Rows.count).End(xlUp))
         If Cl.Value <> "" Then
            Dic(Cl.Value) = Empty
         End If
      Next Cl
   End With

   With Sheets("Plan.Loader").Range("A7").Validation
      .Delete
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=Join(Dic.Keys, ",")
   End With
End Sub
Thanks @Fluff as this also works, but only if Sheets("Plan.2022.02.28") is active. When I am on any other sheet, it gives me the error "Run-time error '1004': Application-defined or object-defined error" for the following line of code:

VBA Code:
or Each Cl In .Range("C9", Range("C" & Rows.Count).End(xlUp))

The code @mumps posted also eliminates the spaces/blank entries.
 
Upvote 0
Thanks @Fluff as this also works, but only if Sheets("Plan.2022.02.28") is active.
You're quite right I missed a period before the 2nd Range on that line, I've edited the code in post#8 to correct that.

The code @mumps posted also eliminates the spaces/blank entries.
It does indeed. :)
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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