Data Validation list that pulls from multiple columns in active row?

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
482
Office Version
  1. 365
Platform
  1. Windows
On my sheet, columns FK:FX of the current row have data. I have a macro that creates a Data Validation list in a specific cell of the currently selected row. Unfortunately, it does not work with the FK:FZ range. If I change it to just FK it works fine. Is it possible to use a range of cells to populate the data validation list?

Here's my code:

VBA Code:
Sub ModelSelect()

    Cells(ActiveCell.Row, "FZ").Activate
    ActiveSheet.Unprotect

    aRange = Cells(ActiveCell.Row, "FK:FX")
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=aRange
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
    
    ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False, AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True

End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
EDIT: Ignore this, it did not work.
 
Last edited:
Upvote 0
Actually, in thinking about this more, I think I would just change your aRange calculation like this:
VBA Code:
Dim aRange as String
aRange = "=$FK$" & ActiveCell.Row & ":$FX$" & ActiveCell.Row
and then everything else should work as you have originally written it.
 
Upvote 0
Another possibility is to eliminate aRange altogether with something like this.

VBA Code:
Sub ModelSelect_v2()
  Cells(ActiveCell.Row, "FZ").Activate
  ActiveSheet.Unprotect
  With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=" & ActiveCell.Offset(, -15).Resize(, 14).Address
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
  End With
  ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False, AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
End Sub

.. or if you do want to refer specifically to the column letters then the relevant line above could be replaced with this
VBA Code:
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=" & Intersect(ActiveCell.EntireRow, Columns("FK:FX")).Address
 
Upvote 0
Another possibility is to eliminate aRange altogether with something like this.

VBA Code:
Sub ModelSelect_v2()
  Cells(ActiveCell.Row, "FZ").Activate
  ActiveSheet.Unprotect
  With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=" & ActiveCell.Offset(, -15).Resize(, 14).Address
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
  End With
  ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False, AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
End Sub

.. or if you do want to refer specifically to the column letters then the relevant line above could be replaced with this
VBA Code:
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=" & Intersect(ActiveCell.EntireRow, Columns("FK:FX")).Address

This works:

VBA Code:
Sub ModelSelect_v2()
  Cells(ActiveCell.Row, "G").Activate
  ActiveSheet.Unprotect
  With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=" & Intersect(ActiveCell.EntireRow, Columns("FK:FX")).Address
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
  End With
  ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False, AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
End Sub

Here's my question. All the cells from FK:FX have functions in them that are used to pull the data. Because of this my Validation List drop down has 14 rows even if there are only 4 values. Is it possible to not show these blank values?
 
Upvote 0
Looks like I need to FILTER?
To do that, I think that you will need some helper columns. I have allowed columns GG:GT as helper columns. These helper columns can be hidden.

VBA Code:
Sub ModelSelect_v3()
  Dim aRange As String
 
  Cells(ActiveCell.Row, "FZ").Activate
  ActiveSheet.Unprotect
  aRange = Intersect(ActiveCell.EntireRow, Columns("FK:FX")).Address
  Cells(ActiveCell.Row, "GG").Formula2 = Replace("=FILTER(#,#<>"""","""")", "#", aRange)
  With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=GG" & ActiveCell.Row & "#"
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
  End With
  ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False, AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
End Sub
 
Last edited:
Upvote 0
BTW, you may not need code at all.
If you put this formula in GG2 (or equivalent formula in whatever the first row it might be needed) and copy down as far as you might need ..
Excel Formula:
=FILTER(FK2:FX2,FK2:FX2<>"","")
.. then in FZ2:FZ?? put the Data Validation as follows ..
1683864025439.png

.. then the DV lists in columns GG, GH, etc will automatically update if any values in FK:FX change and therefore the relevant DV drop-down lists in column FZ will also automatically update.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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