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
Delete Duplicate Rows
Delete Rows And Columns
Worksheet Variables
Loop Through Worksheets
Add Worksheets Dynamically
Find Last Row Or Column
Copy And Paste Special
Copy To Specific Cell Types
Open An Excel File
Open An Excel File w/Params
Open An Excel File On Web
Sort Methods 2003 - 2010
Sort Alpha/Numeric In ASCII
Search Using Match Function
Search Using Vlookup Function
Remove String Non-Printables
Auto_Open And Auto_Close
Initialize Form At Open
Load Combo And List Boxes
Excel Events
Worksheet Change Events
Binary Search Of Array
Typecast Constants
Excel Error Handling
Handling Optional Parameters
Data Validation Drop Downs
Read A Text Fiile w/Handle
Read A Text Fiile w/Script
Dynamically Load Images
Test For Exists Or Open
Loop Through Pictures
Loop Through Form Objects
Splash Screen
Dynamically Load Formulas
Date Examples
Date Find Same Days
Convert Month To Number
Initialize Arrays
Redim An Array
Reassign Button Action
Timer Functions
Legacy Calendar Control
Excel 2010 Date Picker
Paste Pictures Into Excel
Generate Multiple Worksheets
Read Access Data Into Excel

Links To Access Code Examples

Create Recordset With AddNew
Update Field(s) In A Recordset
Import A Tab Delimited File
Export Excel From Query
Import Tab Delim w/WinAPI
Initialize Global Variables
Access Error Handling
Loop Through Form Controls
Insert A Calendar Control
Create A Filtered Recordset
Populate Combo Boxes
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
Parse Delimited Fields
Parameterized Queries (VBA)
Manipulating QueryDefs In VBA
FindFirst On Combined Keys
Execute SQL Delete Records
Commit Form To Table
Report With No Data
Reference Form Objects

 

Date Manipulation

Dates can be manipulated in a way to identify future days of the week, monthly intervals and weekly intervals. The example below shows some of the exotic ways in which dates can be encoded by doing arithmetic on the date values. DateSerial is a powerful tool for computing dates.

Program Code

Option Explicit
Option Base 1

Public Function TestDateManipulation()
Dim dteRunDate As Date
Dim intStartingMonth As Integer
Dim intStartingYear As Integer
Dim intEndingMonth As Integer
Dim intEndingYear As Integer
Dim i As Integer
Dim intColumnHeader As Integer
Dim dteStartingMonday As Date
Dim dteNextMonday As Date
Dim intHeaderColumn As Integer
Dim strL3EndingYYYYWW As String
Dim strL3StartingYYYYWW As String
Dim dteMondayLastYear As Date
Dim dteMondayLastYearPlus18Weeks As Date
Dim strN4StartingYYYYWW As String
Dim strN4EndingYYYYWW As String
Dim dteMonday(14) As Date
Dim strYearWeek(14) As String
Dim strPO14WeeksOutStartYYYYWW As String
Dim strPO14WeeksOutEndingYYYYWW As String

' ********************************************************
' Calculate a 12 Month Interval Moving Back From Today
' ********************************************************
dteRunDate = Cells(1, 1).Value
intStartingMonth = Month(DateSerial(Year(dteRunDate), Month(dteRunDate) - 11, 1))
intStartingYear = Year(DateSerial(Year(dteRunDate), Month(dteRunDate) - 11, 1))
intEndingMonth = Month(dteRunDate)
intEndingYear = Year(dteRunDate)

' ********************************************************
' Translate 1 - 12 into Jan - Dec
' ********************************************************
intHeaderColumn = 1
For i = 1 To 12
    intHeaderColumn = intHeaderColumn + 1
    Cells(1, intHeaderColumn).Value = Format(DateSerial(2000, i, 1), "mmm")
Next i

' ********************************************************
' Other Exotic Examples Of Date Manipulation
' ********************************************************
dteStartingMonday = GetMondayForStartingWeek()
dteNextMonday = dteStartingMonday

strL3EndingYYYYWW = GetYearWeekOfDate(dteStartingMonday - 2)
strL3StartingYYYYWW = GetYearWeekOfDate(dteStartingMonday - 79)
dteMondayLastYear = DateSerial(Year(dteStartingMonday) - 1, Month(dteStartingMonday), Day(dteStartingMonday))
dteMondayLastYearPlus18Weeks = dteMondayLastYear + 125
strN4StartingYYYYWW = GetYearWeekOfDate(dteMondayLastYear)
strN4EndingYYYYWW = GetYearWeekOfDate(dteMondayLastYearPlus18Weeks)

' ***********************************************************************
' Continue With Setting Up The Date Array
' ***********************************************************************
For i = 1 To 14
    dteMonday(i) = dteNextMonday
    strYearWeek(i) = GetYearWeekOfDate(dteNextMonday)
    dteNextMonday = dteNextMonday + 7
Next i

' ***********************************************************************
' Capture PO Values for 90 Days After Week 14
' ***********************************************************************
strPO14WeeksOutStartYYYYWW = GetYearWeekOfDate(dteMonday(14) + 7)
strPO14WeeksOutEndingYYYYWW = GetYearWeekOfDate(dteMonday(14) + 84)
End Function

' ********************************************************
' Get The Monday Date Given Today's Date
' ********************************************************
Public Function GetMondayForStartingWeek() As Date
' ********************************************************
' Sunday Starts A New Week
' ********************************************************

' Using Todays Date, This will Return the Monday of the Starting Week

Dim intStartWeek As Integer
Dim intStartYear As Integer
Dim dteStartMonday As Date

intStartWeek = Format(Date, "ww")
intStartYear = Format(Date, "yyyy")
dteStartMonday = WeekStart(intStartWeek, intStartYear)

GetMondayForStartingWeek = dteStartMonday

End Function

' ********************************************************
' Convert A Date into YYYYWW Format
' ********************************************************
Public Function GetYearWeekOfDate(dteInputDate As Date) As String

' Using The Date Passed, Get the YYYYWW of the date

Dim intStartWeek As Integer
Dim intStartYear As Integer
Dim strStartYearWeek As String

intStartWeek = Format(dteInputDate, "ww")
intStartYear = Format(dteInputDate, "yyyy")
strStartYearWeek = intStartYear & Format(intStartWeek, "0#")


GetYearWeekOfDate = strStartYearWeek

End Function

' ********************************************************
' Get The Start of the Week Date Given Week # & Year
' ********************************************************
Public Function WeekStart(WhichWeek As Integer, WhichYear As _
                    Integer) As Date

WeekStart = YearStart(WhichYear) + ((WhichWeek - 1) * 7)

End Function

' ********************************************************
'
' ********************************************************
Public Function YearStart(WhichYear As Integer) As Date
' ************************************************
' Compute the Date of the First Monday of the Year
' ************************************************
Dim WeekDay As Integer
Dim NewYear As Date

NewYear = DateSerial(WhichYear, 1, 1)

' ************************************************
' The Mod 7 Of A Saturday Is Zero, So To Have
' Monday Be an index of zero, subtract 2
' ************************************************
WeekDay = (NewYear - 2) Mod 7 'Generate weekday index where Monday = 0 If WeekDay < 4 Then YearStart = NewYear - WeekDay Else YearStart = NewYear - WeekDay + 7 End If End Function