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

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
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.
@Fluff Ah. I should have been able to catch that. After the adjustment it also works. Thanks again for your continued help.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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