Logicwurks Home Page

Links To Excel Code Examples

Tracing VBA Statements
Range/Wkb/Wks Variables
Add Grand Totals Using Ranges
Using Range Offset Property
Using Range Find Method
ConvertCellAddressToRange
Set Conditional Formatting
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
ImportExternalWorksheets
Find Last Row Or Column
Copy And Paste Special
Copy To Specific Cell Types
Range Copy With Filter
ExcelFileOpenSaveClose
ExcelFileOpenSaveCSV
Open An Excel File
Open An Excel File w/Params
Open An Excel File On Web
Save A Workbook
Save A Workbook Using mso
Clone A Workbook
Test If WEB URL Exists
Parse Using Split Command
Using Classes in Excel
TypeStatementStructures
Color Management
Convert Cell Color To RGB
Sort Methods 2003 - 2010
Sort Alpha/Numeric In ASCII
Search Using Match Function
Search Using Vlookup Function
Search Using Xlookup 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
Insert Data Validation Sub
Read A Text File w/Handle
Write A Text File w/Handle
Read a Binary File w/Handle
Update a Binary File w/Handle
Binary File Copy and Update
Read A Text Fiile w/Script
Text File Processing Examples
Test For Exists Or Open
Splash Screen
Dynamically Load Formulas
PaymentStreamsByDate
Date Examples
Date Find Same Days
Convert Month To Number
Initialize Arrays
Load Arrays Using Evaluate
ChartsAndGraphsVBA
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
Insert Cell Comments

Links To Access Code Examples

DAO Versus ADODB
SearchVBACodeStrings
Interface Excel With Access
Create Form Manually
Create Recordset With AddNew
Multi-Select List Boxes
Update Field(s) In A Recordset
Update Excel Pivot From Access
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
Control Excel via Access VBA
On Error Best Practices
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
Dlookup Command
Dlookup In Form Datasheet
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

 

Date Picker Alternative

Date pickers and calendar controls can be an issue in a large audience of Excel application users. The reason is that the necessary Active-X files may not be registered and will cause an application written using mscal.ocx or mscomct2.ocx to fail.

The code below uses VBA date functions to provide a primitive but effective calendar picker for an application that requires the user to select a beginning month for financial projections. After the month is selected, the application then assigns 12 month-end dates, 8 quarter-end dates and 2 year-end dates. These dates are then used to update headers in a number of financial projection worksheets.

The combo-box provides a total of 24 monthly dates from which the user can select the required month for the start of the business projection. These 24 dates are in relation to today's date.

You can also build an elaborate date picker alternative using the method suggested in this link:

Date Picker Alternative

Program Code

Option Explicit
Dim strTodaysMonthEndAlpha As String
Dim dteMonthlyDatesForDropDown(1 To 24) As Date
Dim strMonthlyDatesForDropDownAlpha(1 To 24) As String
Dim strMonthlyDatesForWorksheet(1 To 12) As String
Dim strQuarterlyDatesForWorksheet(1 To 8) As String
Dim strYearlyDatesForWorksheet(1 To 5) As String
Dim dteStartingDateForMonthly As Date
Dim dteStartingDateForQuarterly As Date

Dim wkbProjections As Workbook
Dim wksAssumptions As Worksheet
Dim wksIncomeStatement As Worksheet
Dim wksBalanaceSheet As Worksheet
Dim wksCashFlow As Worksheet
Dim wksROI As Worksheet
Dim wksBreakEvenWhatIf As Worksheet
Dim wksIncomeStatementAnnual As Worksheet
Dim wksBalanceSheetAnnual As Worksheet
Dim wksCashFlowAnnual As Worksheet

Dim i As Integer
Dim intStartingMonthIndex As Integer

Private Sub cmdProceedWithLoad_Click()
' *******************************************************************************
' Create Calendar Array Entries For Populating The Worksheets
' *******************************************************************************
intStartingMonthIndex = Me.cmbSelectMonth.ListIndex + 1
dteStartingDateForMonthly = dteMonthlyDatesForDropDown(intStartingMonthIndex)

For i = 1 To 12
    strMonthlyDatesForWorksheet(i) = Format(DateSerial(Year(dteStartingDateForMonthly), _
                                     Month(dteStartingDateForMonthly) + i, 0), "Mmmm dd, YYYY")
Next i

For i = 1 To 8
    strQuarterlyDatesForWorksheet(i) = Format(DateSerial(Year(dteStartingDateForMonthly), _
                                    Month(dteStartingDateForMonthly) + (3 * i), 0), "Mmmm dd, YYYY")
Next i

For i = 1 To 5
    strYearlyDatesForWorksheet(i) = Format(DateSerial(Year(dteStartingDateForMonthly), _
                                    Month(dteStartingDateForMonthly) + (12 * i), 0), "Mmmm dd, YYYY")
Next i

' *******************************************************************************
' Initialize Workbook And Worksheet Variables
' *******************************************************************************
Set wkbProjections = ThisWorkbook
Set wksAssumptions = wkbProjections.Sheets("Assumptions")
Set wksIncomeStatement = wkbProjections.Sheets("Inc Stmt")
Set wksBalanaceSheet = wkbProjections.Sheets("Bal Sheet")
Set wksCashFlow = wkbProjections.Sheets("Cash Flow")
Set wksROI = wkbProjections.Sheets("ROI")
Set wksBreakEvenWhatIf = wkbProjections.Sheets("Brk Ev What If")
Set wksIncomeStatementAnnual = wkbProjections.Sheets("Inc Stmt Annual")
Set wksBalanceSheetAnnual = wkbProjections.Sheets("Bal Sheet Annual")
Set wksCashFlowAnnual = wkbProjections.Sheets("Cash Flow Annual")

' *******************************************************************************
' Update The Worksheet Headers
' *******************************************************************************
Call UpdateWorksheetsWithAllDates(wksAssumptions, 4, 2)
Call UpdateWorksheetsWithAllDates(wksIncomeStatement, 7, 2)
Call UpdateWorksheetsWithAllDates(wksBalanaceSheet, 7, 2)
Call UpdateWorksheetsWithAllDates(wksCashFlow, 6, 2)
Call UpdateWorksheetsWithYearVertical(wksROI, 6, 1)
Call UpdateWorksheetsWithYearHorizontal(wksBreakEvenWhatIf, 6, 2)
Call UpdateWorksheetsWithYearHorizontal(wksBreakEvenWhatIf, 32, 2)
Call UpdateWorksheetsWithYearHorizontal(wksIncomeStatementAnnual, 7, 2)
Call UpdateWorksheetsWithYearHorizontal(wksBalanceSheetAnnual, 7, 2)
Call UpdateWorksheetsWithYearHorizontal(wksCashFlowAnnual, 7, 2)

Unload Me

End Sub

Private Sub UserForm_Initialize()
' **************************************************************************************
' Compute The Dates To Be Used For the Drop Down Date Selector
' **************************************************************************************

strTodaysMonthEndAlpha = Format(DateSerial(Year(Date), Month(Date) + 1, 0), "Mmmm YYYY")

Me.cmbSelectMonth.Clear

For i = 1 To 24
    dteMonthlyDatesForDropDown(i) = DateSerial(Year(Date), Month(Date) - 6 + i, 0)
    strMonthlyDatesForDropDownAlpha(i) = Format(DateSerial(Year(Date), Month(Date) - 6 + i, 0), "Mmmm YYYY")
    Me.cmbSelectMonth.AddItem (strMonthlyDatesForDropDownAlpha(i))
Next i

Me.cmbSelectMonth.Value = strTodaysMonthEndAlpha

End Sub


Private Sub UpdateWorksheetsWithAllDates(SheetToUpdate As Worksheet, RowToUpdate As Long, ColumnToStart As Long)
' *******************************************************************************
' Update Worksheet Header Dates (Format 1)
' *******************************************************************************
Dim lngColumnToUpdate As Long
lngColumnToUpdate = ColumnToStart - 1

For i = 1 To 12
    lngColumnToUpdate = lngColumnToUpdate + 1
    SheetToUpdate.Cells(RowToUpdate, lngColumnToUpdate).Value = strMonthlyDatesForWorksheet(i)
Next i

For i = 1 To 8
    lngColumnToUpdate = lngColumnToUpdate + 1
    SheetToUpdate.Cells(RowToUpdate, lngColumnToUpdate).Value = strQuarterlyDatesForWorksheet(i)
Next i

For i = 1 To 2
    lngColumnToUpdate = lngColumnToUpdate + 1
    SheetToUpdate.Cells(RowToUpdate, lngColumnToUpdate).Value = strYearlyDatesForWorksheet(i)
Next i

End Sub


Private Sub UpdateWorksheetsWithYearHorizontal(SheetToUpdate As Worksheet, RowToUpdate As Long, ColumnToStart As Long)
' *******************************************************************************
' Update Worksheet Header Dates (Format 2)
' *******************************************************************************
Dim lngColumnToUpdate As Long
lngColumnToUpdate = ColumnToStart - 1

For i = 1 To 5
    lngColumnToUpdate = lngColumnToUpdate + 1
    SheetToUpdate.Cells(RowToUpdate, lngColumnToUpdate).Value = strYearlyDatesForWorksheet(i)
Next i

End Sub

Private Sub UpdateWorksheetsWithYearVertical(SheetToUpdate As Worksheet, RowToStart As Long, ColumnToUpdate As Long)
' *******************************************************************************
' Update Worksheet Header Dates (Format 3)
' *******************************************************************************
Dim lngRowToUpdate As Long
lngRowToUpdate = RowToStart - 1

For i = 1 To 5
    lngRowToUpdate = lngRowToUpdate + 1
    SheetToUpdate.Cells(lngRowToUpdate, ColumnToUpdate).Value = strYearlyDatesForWorksheet(i)
Next i

End Sub