Logicwurks Home Page

Links To Excel Code Examples

Range/Wkb/Wks Variables
Add Grand Totals Using Ranges
Using Range Offset Property
Using Range Find Method
Union Of Ranges
Parse Range Strings
Delete Duplicate Rows
Delete Rows And Columns
Worksheet Variables
TypeName And TypeOf
Loop Through Worksheets
Loop Through Open Workbooks
Form Button Magic
Command Button Magic
Add Worksheets Dynamically
Find Last Row Or Column
Copy And Paste Special
Copy To Specific Cell Types
Range Copy With Filter
Open An Excel File
Open An Excel File w/Params
Open An Excel File On Web
Save A Workbook
Clone A Workbook
Test If WEB URL Exists
Parse Using Split Command
Color Management
Convert Cell Color To RGB
Sort Methods 2003 - 2010
Sort Alpha/Numeric In ASCII
Search Using Match Function
Search Using Vlookup Function
Using Find Instead of Vlookup
Remove String Non-Printables
Auto_Open And Auto_Close
Initialize Form At Open
Edit Numerics In UserForm
Load Combo And List Boxes
Floating Sheet Combo Boxes
Advanced User Form Coding
Excel Events
Worksheet Change Events
Binary Search Of Array
Typecast Constants
Excel Error Handling
Handling Optional Parameters
Data Validation Drop Downs
Read A Text File w/Handle
Write A Text File w/Handle
Read A Text Fiile w/Script
Text File Processing Examples
Test For Exists Or Open
Splash Screen
Dynamically Load Formulas
Date Examples
Date Find Same Days
Convert Month To Number
Initialize Arrays
Load Arrays Using Evaluate
Redim An Array
Reassign Button Action
Timer Functions
Legacy Calendar Control
Excel 2010 Date Picker
Date Picker Alternative
Generate Multiple Worksheets
Read Access Data Into Excel
Send Outlook Email w/Attach
Copy AutoFilters To Sheets
Export A Text File
Get Windows User Name
VBA Format Statement
Manipulate Files via VBA
Dynamically Load Images
Loop Through Worksheet Objects
Loop Through Form Objects
Loop Through Files with DIR
Active-X Checkboxes
Add Forms Checkboxes Dynam
Paste Pictures Into Excel
Copy Pictures Sheet To Sheet
Copy Pictures Sheet To Sheet
Create Forms Buttons With VBA
Extract Filename From Path
Convert R1C1 Format to A1
Special Cells Property

Links To Access Code Examples

Create Recordset With AddNew
Multi-Select List Boxes
Update Field(s) In A Recordset
Import A Tab Delimited File
Export Excel FileDialog
Create Excel Within Access
Open Excel Within Access
Open Excel OBJ From Access
Format Excel From Access
Import Tab Delim w/WinAPI
Initialize Global Variables
Using TempVars For Globals
Access Error Handling
Loop Through Form Controls
Insert A Calendar Control
Create A Filtered Recordset
Populate Combo Boxes
Bookmarks And Forms
Combo Box Multiple Sources
Passing Form Objects
Create VBA SQL Statements
Create Dynamic Queries
Display File Images On A Form
Manipulate Files via VBA
Manipulate Files via Scripting
Number Subform Records
Reference Subform Objects
Parse Delimited Fields
Parameterized Queries (VBA)
Manipulating QueryDefs In VBA
FindFirst On Combined Keys
Using The Dlookup Command
Execute SQL Delete Records
Commit Form To Table
Report With No Data
Reference Form Objects
DSNLess Connections To MySQL
Print Active Form Record
Count Records in Linked Tables
Delete Empty Tables
Open Linked SQL Tables

 

Create Dynamic Data Validation Drop Downs Using VBA

When a user enters values in a worksheet cell, data validation from a list provides a drop-down that the user can select to enter the data, ensuring consistent values.


These drop-down validations can be created from VBA instead of hand-coded by the user. In the VBA code illustrated below, the validation drop-down lists are not derived from literals, but reference a range of values from a separate "Validation Profile" worksheet (in the same workbook).

The next illustration shows a sample "Validation Profile" worksheet, in which the proper "list of values" for validation is created for six different fields: Technician, Time, City, Type, Home and By.

"City", for example, uses a validation range of "D3 through D17" as the correct list of values.

When the VBA code creates validations on a "Master Schedule" worksheet, it references the range of values for each of the six distinct columns (B through G) in the "Validation Profile" worksheet. The code example populates drop-downs in 6 different columns on the "Master Schedule" worksheet by referencing ranges of values on the "Validation Profile" worksheet.

As each new row on the "Master Schedule" worksheet is created, the drop downs pertaining to that row are also created, and in addition, the entire technician row color is formatted to match the colors in Column A of the "Validation Profile" worksheet.

Program Code

Option Explicit
' ********************************************************
' Workbook and Worksheet Variables
' ********************************************************
Dim wkbSchedule As Workbook
Dim wksMainCalendar As Worksheet
Dim wksProfile As Worksheet

' ********************************************************
' Row Counters
' ********************************************************
Dim lngNumberOfScheduleRows As Long
Dim lngScheduleCurrentRow As Long
Dim lngNumberOfProfileTechs As Long
Dim lngNumberOfProfileTimes As Long
Dim lngNumberOfProfileCities As Long
Dim lngNumberOfProfileTypes As Long
Dim lngNumberOfProfileHomes As Long
Dim lngNumberOfProfileBy As Long

' ********************************************************
' Drop Down Formulas
' ********************************************************
Dim strTechFormula As String
Dim strTimeFormula As String
Dim strCityFormula As String
Dim strTypeFormula As String
Dim strHomeFormula As String
Dim strByFormula As String

' ********************************************************
' Other Variables
' ********************************************************
Dim dteLastDateOnSchedule As Date
Dim dteScheduleDate As Date
Dim dteEndDate As Date
Dim lngThemeColor As Long
Dim dblTintAndShade As Double
Dim lngColumn As Long
Dim strDayOfWeek As String

Public Sub CreateANewCalendarMonth()


' ********************************************************
' Application Screen Updating
' ********************************************************
Application.ScreenUpdating = False

' ********************************************************
' Initialize Workbook and Worksheet Variables
' ********************************************************
Set wkbSchedule = ThisWorkbook
Set wksMainCalendar = wkbSchedule.Sheets(1)
Set wksProfile = wkbSchedule.Sheets("Profiles")

' ********************************************************
' Count Rows In Main Calendar And Profiles
' ********************************************************
lngNumberOfScheduleRows = wksMainCalendar.Cells(Rows.Count, "A").End(xlUp).Row
lngNumberOfProfileTechs = wksProfile.Cells(Rows.Count, "B").End(xlUp).Row
lngNumberOfProfileTimes = wksProfile.Cells(Rows.Count, "C").End(xlUp).Row
lngNumberOfProfileCities = wksProfile.Cells(Rows.Count, "D").End(xlUp).Row
lngNumberOfProfileTypes = wksProfile.Cells(Rows.Count, "E").End(xlUp).Row
lngNumberOfProfileHomes = wksProfile.Cells(Rows.Count, "F").End(xlUp).Row
lngNumberOfProfileBy = wksProfile.Cells(Rows.Count, "G").End(xlUp).Row

' ********************************************************
' Create The DropDown Formulas
' ********************************************************
strTechFormula = "=Profiles!$B$3:$B$" & lngNumberOfProfileTechs
strTimeFormula = "=Profiles!$C$3:$C$" & lngNumberOfProfileTimes
strCityFormula = "=Profiles!$D$3:$D$" & lngNumberOfProfileCities
strTypeFormula = "=Profiles!$E$3:$E$" & lngNumberOfProfileTypes
strHomeFormula = "=Profiles!$F$3:$F$" & lngNumberOfProfileHomes
strByFormula = "=Profiles!$G$3:$G$" & lngNumberOfProfileBy

lngScheduleCurrentRow = lngNumberOfScheduleRows + 1

If Not IsDate(wksMainCalendar.Cells(lngNumberOfScheduleRows, 1)) Then
    MsgBox ("Last Row Column A Is Not A Valid Date - Please Correct")
    Exit Sub
End If

' ********************************************************
' Get Last Date
' ********************************************************
dteLastDateOnSchedule = wksMainCalendar.Cells(lngNumberOfScheduleRows, 1)
dteScheduleDate = dteLastDateOnSchedule

' ********************************************************
' Calculate Current Month End Point
' ********************************************************
If Day(dteScheduleDate) > 15 Then
    dteEndDate = DateSerial(Year(dteScheduleDate), Month(dteScheduleDate) + 2, 0)
Else
    dteEndDate = DateSerial(Year(dteScheduleDate), Month(dteScheduleDate) + 1, 0)
End If

' ********************************************************
' Make Sure End Point is Not Saturday or Sunday
' ********************************************************
If Format(dteEndDate, "ddd") = "Sat" Then
    dteEndDate = dteEndDate - 1
ElseIf Format(dteEndDate, "ddd") = "Sun" Then
    dteEndDate = dteEndDate - 2
End If

' ********************************************************
' Start With The Next Day After The Last Date
' ********************************************************
dteScheduleDate = dteScheduleDate + 1

' ********************************************************
' Add Monthly Header
' ********************************************************
wksMainCalendar.Range("A1:T1").Copy wksMainCalendar.Cells(lngScheduleCurrentRow, 1)
wksMainCalendar.Rows(lngScheduleCurrentRow).RowHeight = 31.5

' ********************************************************
' Loop Through All The Days, Skipping Saturday And Sunday
' ********************************************************
Do While dteScheduleDate <= dteEndDate

If Format(dteScheduleDate, "ddd") = "Sat" Or _
   Format(dteScheduleDate, "ddd") = "Sun" Then
   GoTo DateLoop
End If

Call CreateSingleDaySchedule

DateLoop:
dteScheduleDate = dteScheduleDate + 1
Loop

' ********************************************************
' Finish Out Until Friday
' ********************************************************
Do While Format(dteScheduleDate, "ddd") <> "Sat"
Call CreateSingleDaySchedule
dteScheduleDate = dteScheduleDate + 1
Loop

' ********************************************************
' Go To The Top of the Main Worksheet
' ********************************************************
wksMainCalendar.Select
wksMainCalendar.Cells(1, 1).Select

' ********************************************************
' Application Screen Updating
' ********************************************************
Application.ScreenUpdating = True

End Sub

Private Sub CreateSingleDaySchedule()
Dim i As Long

For i = 3 To lngNumberOfProfileTechs
' ********************************************************
' Row 1 Of Tech
' ********************************************************
    lngScheduleCurrentRow = lngScheduleCurrentRow + 1
    Call PopulateDropDowns(strTechFormula, 2)
    Call PopulateDropDowns(strTimeFormula, 4)
    Call PopulateDropDowns(strCityFormula, 9)
    Call PopulateDropDowns(strTypeFormula, 13)
    Call PopulateDropDowns(strHomeFormula, 14)
    Call PopulateDropDowns(strByFormula, 19)
    wksMainCalendar.Cells(lngScheduleCurrentRow, 1).Value = dteScheduleDate
    wksMainCalendar.Cells(lngScheduleCurrentRow, 2).Value = wksProfile.Cells(i, 2).Value
    
    strDayOfWeek = Format(dteScheduleDate, "ddd")
    If strDayOfWeek = "Thu" Then
        strDayOfWeek = "Th"
    Else
        strDayOfWeek = Left(strDayOfWeek, 1)
    End If
    wksMainCalendar.Cells(lngScheduleCurrentRow, 3).Value = strDayOfWeek
    
    wksMainCalendar.Cells(lngScheduleCurrentRow, 4).Value = "8:30-9:30"
    lngThemeColor = wksProfile.Cells(i, 1).Interior.ThemeColor
    dblTintAndShade = wksProfile.Cells(i, 1).Interior.TintAndShade
    Range(wksMainCalendar.Cells(lngScheduleCurrentRow, 1), wksMainCalendar.Cells(lngScheduleCurrentRow, 20)).Interior.ThemeColor = lngThemeColor
    Range(wksMainCalendar.Cells(lngScheduleCurrentRow, 1), wksMainCalendar.Cells(lngScheduleCurrentRow, 20)).Interior.TintAndShade = dblTintAndShade

' ********************************************************
' Row 2 Of Tech
' ********************************************************
    lngScheduleCurrentRow = lngScheduleCurrentRow + 1
    Call PopulateDropDowns(strTechFormula, 2)
    Call PopulateDropDowns(strTimeFormula, 4)
    Call PopulateDropDowns(strCityFormula, 9)
    Call PopulateDropDowns(strTypeFormula, 13)
    Call PopulateDropDowns(strHomeFormula, 14)
    Call PopulateDropDowns(strByFormula, 19)

' ********************************************************
' Row 3 Of Tech
' ********************************************************
    lngScheduleCurrentRow = lngScheduleCurrentRow + 1
    Call PopulateDropDowns(strTechFormula, 2)
    Call PopulateDropDowns(strTimeFormula, 4)
    Call PopulateDropDowns(strCityFormula, 9)
    Call PopulateDropDowns(strTypeFormula, 13)
    Call PopulateDropDowns(strHomeFormula, 14)
    Call PopulateDropDowns(strByFormula, 19)
    wksMainCalendar.Cells(lngScheduleCurrentRow, 1).Value = dteScheduleDate
    wksMainCalendar.Cells(lngScheduleCurrentRow, 2).Value = wksProfile.Cells(i, 2).Value
    wksMainCalendar.Cells(lngScheduleCurrentRow, 3).Value = strDayOfWeek
    wksMainCalendar.Cells(lngScheduleCurrentRow, 4).Value = "12:30-1:30"
    lngThemeColor = wksProfile.Cells(i, 1).Interior.ThemeColor
    dblTintAndShade = wksProfile.Cells(i, 1).Interior.TintAndShade
    Range(wksMainCalendar.Cells(lngScheduleCurrentRow, 1), wksMainCalendar.Cells(lngScheduleCurrentRow, 20)).Interior.ThemeColor = lngThemeColor
    Range(wksMainCalendar.Cells(lngScheduleCurrentRow, 1), wksMainCalendar.Cells(lngScheduleCurrentRow, 20)).Interior.TintAndShade = dblTintAndShade

' ********************************************************
' Row 4 Of Tech
' ********************************************************
    lngScheduleCurrentRow = lngScheduleCurrentRow + 1
    Call PopulateDropDowns(strTechFormula, 2)
    Call PopulateDropDowns(strTimeFormula, 4)
    Call PopulateDropDowns(strCityFormula, 9)
    Call PopulateDropDowns(strTypeFormula, 13)
    Call PopulateDropDowns(strHomeFormula, 14)
    Call PopulateDropDowns(strByFormula, 19)

Next i

End Sub

Private Sub PopulateDropDowns(strProfileDropDown, lngColumnParam)
    With wksMainCalendar.Cells(lngScheduleCurrentRow, lngColumnParam).Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertInformation, Operator:= _
         xlBetween, Formula1:=strProfileDropDown
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = False
        .ShowError = False
    End With

End Sub