Assign data validation drop down list using range of cells

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi All, How to assign a range of cells to the dropdown list ? FORMULA1 seems not recognize the range of cells !
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If Sh.Name = "Data" Then Exit Sub


Dim rngg As Range
Dim lr As Long
lr = Range("A2").End(xlDown).Row
Set rngg = Range("C3:AL" & lr)

On Error Resume Next

If Target.CountLarge > 1 Then Exit Sub

If Not Intersect(Target, rngg) Is Nothing Then

With rngg.Validation
 .Delete
 .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
  Operator:=xlBetween, Formula1:=Sheets("Data").Range("G1:G" & Range("G1").End(xlDown).Row)   

 .IgnoreBlank = True
 .InCellDropdown = Truem
 .InputTitle = ""
 .ErrorTitle = ""
 .InputMessage = ""
 .ErrorMessage = ""
 .ShowInput = True
 .ShowError = True
End With

End If

End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You may need to qualify the sheet that range is on:
Formula1:=Sheets("Data").Range("G1:G" & Sheets("Data").cells(rows.Count,"G").end(xlup).Row)
 
Upvote 0
Hi All, How to assign a range of cells to the dropdown list ? FORMULA1 seems not recognize the range of cells !
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If Sh.Name = "Data" Then Exit Sub


Dim rngg As Range
Dim lr As Long
lr = Range("A2").End(xlDown).Row
Set rngg = Range("C3:AL" & lr)

On Error Resume Next

If Target.CountLarge > 1 Then Exit Sub

If Not Intersect(Target, rngg) Is Nothing Then

With rngg.Validation
 .Delete
 .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
  Operator:=xlBetween, Formula1:=Sheets("Data").Range("G1:G" & Range("G1").End(xlDown).Row)  

 .IgnoreBlank = True
 .InCellDropdown = Truem
 .InputTitle = ""
 .ErrorTitle = ""
 .InputMessage = ""
 .ErrorMessage = ""
 .ShowInput = True
 .ShowError = True
End With

End If

End Sub
@Vincent88 This post may assist you: VBA Code for Data Validation on Another Sheet.

Also, in your line of code: Sh.Name = "Data", don't you need something like the following before it so you get the sheet name and test it.
Dim Sh.Name as String
Sh.Name = ActiveSheet.Name

Also, how are you getting to the active sheet? I assume you posted only part of your code. I always take my code and make another workbook and test it to make sure somebody can take my code and test it easily on a workbook of their own.
 
Upvote 0
Also, how are you getting to the active sheet? I assume you posted only part of your code. I always take my code and make another workbook and test it to make sure somebody can take my code and test it easily on a workbook of their own.
@OilEconomist Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range), if code is placed properly, will refer to the active sheet. ;)
 
Upvote 0

Forum statistics

Threads
1,215,005
Messages
6,122,661
Members
449,091
Latest member
peppernaut

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