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
Insert Cell Comments

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
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 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