» Validation list without empty cells using VBA in Microsoft Excel
CATEGORY - General Topics in VBA
VERSION - All Microsoft Excel Versions
Question: The validity list in cell A1 shall contain data from column A from Sheet2 without empty rows and shall be adjustable.
Answer:
Insert the following code in This Workbook module.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim intRow As Integer, intLastRow As Integer
Dim txt As String
If Target.Address <> "$A$1" Then Exit Sub
With Worksheets("Sheet2")
intLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
For intRow = 1 To intLastRow
If Not IsEmpty(.Cells(intRow, 1)) Then
txt = txt & .Cells(intRow, 1) & ","
End If
Next intRow
End With
txt = Left(txt, Len(txt) - 1)
With Range("A1").Validation
.Delete
.Add _
Type:=xlValidateList, _
AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, _
Formula1:=txt
End With
End Sub
Book Store:
Recommended Books:
- Special Edition Using Microsoft Excel 2002
- VBA for Modelers: Developing Decision Support Systems Using Microsoft« Excel
- Microsoft Access 2002 for Dummies
- Successful Business Planning in 30 Days: A Step-By-Step Guide for Writing a Business Plan and Starting Your Own Business
- Financial Shenanigans : How to Detect Accounting Gimmicks & Fraud in Financial Reports
- What the IRS Doesn't Want You to Know: A Cpa Reveals the Tricks of the Trade
No comments have been submitted.

