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

 

Redim An Array To Load Combo Box

For complete information on arrays, see Arrays

Many programs will process a variable number if items to load into a form's combo box. Using the Redim command, an array can be "max sized", and then based on the actual number of items encountered in the data, it can be redimensioned to the exact number of items in the array. This redimensioning is necessary to load a combo box using an efficient "one line" load statement.

A combo box can also be loaded "one item at a time", but bulk loading using a redimensioned array is the fastest method and requires less code.

Program Code

Option Explicit
' ***************************************************************
' Use Option Base 1 Since I Start Indexing at 1 and not 0
' ***************************************************************
Option Base 1

' ***************************************************************
' Define The Array With No Value For the Number Of Entries
' ***************************************************************
Public strProductID() As String

Public Sub DemoRedimArray()

' ***************************************************************
' Before the Array is Used, It Must Be Dimensioned
' To A Maximum Value For The Number Of Entries
' That Could Be Loaded
' ***************************************************************
ReDim strProductID(200)

' ***************************************************************
' Dynamically Load Values - Usually Done With A Counter
' But For Demo Purposes, Hard Coded Values Are Used
' ***************************************************************
strProductID(1) = "AAAAA"
strProductID(2) = "BBBBB"
strProductID(3) = "CCCCC"
strProductID(4) = "DDDDD"
strProductID(5) = "EEEEE"

' ***************************************************************
' When The Number of ACTUAL Entries Loaded in the Array
' Has Been Determined, REDIM The Array with a Preserve
' ***************************************************************
ReDim Preserve strProductID(5)

' ***************************************************************
' Load A Form's Combo Box With The Exact Number Of Array Entries
' ***************************************************************
frmProducts.cmbProductID.List = strProductID

' ***************************************************************
' Display The Form And Its Entries
' ***************************************************************

frmProducts.Show

End Sub

Dynamically Redim Arrays As They Are Loaded

The following code illustrates how to REDIM PRESERVE an array as it is loaded so that the dimensions are always up to date.

Program Code

Public Sub UpdateScheduleFromJobSource()
' ********************************************************
' Define Known Workbook and Worksheet Variables
' ********************************************************
Dim wkbJobSchedule As Workbook
Dim wksJobSchedule As Worksheet
Dim wksListDropDown As Worksheet
Dim wksJobSourceToCopyFrom As Worksheet
Dim wksTestIfSheetExists As Worksheet

' ********************************************************
' Define Variable Job Source Worksheets
' ********************************************************
Dim strJobSourceWorksheetName() As String
Dim intJobSourceWorksheetIndex() As Integer
Dim wksJobSource() As Worksheet

' ********************************************************
' Other Variables
' ********************************************************
Dim intJobSourceWorksheetCount As Integer
Dim lngLastTargetJobSourceWorksheetRow As Long
Dim strJobSourceWorksheetNameForCopy As String
Dim lngJobSourceRowToBeCopied As Long
Dim lngJobScheduleTargetRow As Long
Dim lngMessageResponse As Long

Dim i As Long

' ********************************************************
' Define Target Row in Job Source
' ********************************************************
If ActiveSheet.Name <> "Schedule" Then
    MsgBox ("1> You Must Select A Job Source Cell" & vbCrLf & _
            "2> Then Press Alt-F8 To Run The Macro Called a_MarkJobSourceLocation" & vbCrLf & _
            "3> Then Select The Target Job Schedule Line To Be Updated" & vbCrLf & _
            "4> Then Click The 'Update Job Schedule Line From Job Source' Button")
            strJobSourceNameToBeUpdated = ""
            lngJobSourceRowToUpdate = 0
    Exit Sub
End If

lngJobScheduleTargetRow = ActiveCell.Row

If lngJobScheduleTargetRow < 3 Then
    MsgBox ("1> You Must Select A Job Source Cell" & vbCrLf & _
            "2> Then Press Alt-F8 To Run The Macro Called a_MarkJobSourceLocation" & vbCrLf & _
            "3> Then Select The Target Job Schedule Line To Be Updated" & vbCrLf & _
            "4> Then Click The 'Update Job Schedule Line From Job Source' Button")
            strJobSourceNameToBeUpdated = ""
            lngJobSourceRowToUpdate = 0
    Exit Sub
End If

' ***********************************************
' Make Sure A Job Source Worksheet Line Was
' Marked As The Source Of The Update
' ***********************************************
If strJobSourceNameToBeUpdated = "" Or lngJobSourceRowToUpdate = 0 Then
    MsgBox ("1> You Must Select A Job Source Cell" & vbCrLf & _
            "2> Then Press Alt-F8 To Run The Macro Called a_MarkJobSourceLocation" & vbCrLf & _
            "3> Then Select The Target Job Schedule Line To Be Updated" & vbCrLf & _
            "4> Then Click The 'Update Job Schedule Line From Job Source' Button")
            strJobSourceNameToBeUpdated = ""
            lngJobSourceRowToUpdate = 0
    Exit Sub
End If

' ***********************************************
' Change Names To Match Actual Function
' ***********************************************
strJobSourceWorksheetNameForCopy = strJobSourceNameToBeUpdated
lngJobSourceRowToBeCopied = lngJobSourceRowToUpdate

' ***********************************************
' Initialize The Workbook and Worksheet Variables
' ***********************************************
Set wkbJobSchedule = ThisWorkbook
Set wksJobSchedule = wkbJobSchedule.Sheets("Schedule")
Set wksListDropDown = wkbJobSchedule.Sheets("ListDropDown")

intJobSourceWorksheetCount = 0
lngLastTargetJobSourceWorksheetRow = wksListDropDown.Cells(Rows.Count, "W").End(xlUp).Row

For i = 4 To lngLastTargetJobSourceWorksheetRow
    On Error Resume Next
    Set wksTestIfSheetExists = Nothing
    Set wksTestIfSheetExists = Sheets(wksListDropDown.Cells(i, 23).Value)
    If wksTestIfSheetExists Is Nothing Then
        MsgBox ("Job Source Worksheet " & wksListDropDown.Cells(i, 23).Value & " Does Not Exist")
        Err.Clear
    Else
        On Error GoTo 0
        intJobSourceWorksheetCount = intJobSourceWorksheetCount + 1
        ReDim Preserve strJobSourceWorksheetName(1 To intJobSourceWorksheetCount)
        ReDim Preserve intJobSourceWorksheetIndex(1 To intJobSourceWorksheetCount)
        ReDim Preserve wksJobSource(1 To intJobSourceWorksheetCount)
        strJobSourceWorksheetName(intJobSourceWorksheetCount) = wksListDropDown.Cells(i, 23).Value
        intJobSourceWorksheetIndex(intJobSourceWorksheetCount) = wksTestIfSheetExists.Index
        Set wksJobSource(intJobSourceWorksheetCount) = Sheets(intJobSourceWorksheetIndex(intJobSourceWorksheetCount))
    End If
Next i

On Error GoTo 0

' ***********************************************
' Validate The Job Source Worksheet Is Valid
' ***********************************************
Set wksJobSourceToCopyFrom = Nothing
For i = 1 To UBound(strJobSourceWorksheetName)
    If strJobSourceWorksheetNameForCopy = strJobSourceWorksheetName(i) Then
        Set wksJobSourceToCopyFrom = wksJobSource(i)
        Exit For
    End If
Next i

If wksJobSourceToCopyFrom Is Nothing Then
    MsgBox ("1> You Must Select A Job Source Cell" & vbCrLf & _
            "2> Then Press Alt-F8 To Run The Macro Called a_MarkJobSourceLocation" & vbCrLf & _
            "3> Then Select The Target Job Schedule Line To Be Updated" & vbCrLf & _
            "4> Then Click The 'Update Job Schedule Line From Job Source' Button")
            strJobSourceNameToBeUpdated = ""
            lngJobSourceRowToUpdate = 0
    Exit Sub
End If

lngMessageResponse = MsgBox("You Are About To Update Job Schedule Row " & lngJobScheduleTargetRow & vbCrLf & _
                            "From Job Source Worksheet " & strJobSourceWorksheetNameForCopy & ", Row " & _
                            lngJobSourceRowToBeCopied & vbCrLf & _
                            "Is This Correct?", vbYesNo, "Update Job Schedule From Job Source")

If lngMessageResponse = vbNo Then
    strJobSourceNameToBeUpdated = ""
    lngJobSourceRowToUpdate = 0
    Exit Sub
End If

' ***********************************************
' Update The Job Schedule From The Job Source
' ***********************************************
wksJobSchedule.Cells(lngJobScheduleTargetRow, 2).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 32)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 6).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 5)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 7).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 6)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 8).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 7)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 9).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 8)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 10).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 9)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 11).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 10)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 12).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 11)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 13).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 12)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 14).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 13)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 15).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 14)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 16).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 15)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 17).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 16)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 18).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 17)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 19).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 18)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 20).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 19)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 25).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 22)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 26).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 33)
wksJobSchedule.Cells(lngJobScheduleTargetRow, 27).Value = wksJobSourceToCopyFrom.Cells(lngJobSourceRowToBeCopied, 34)

End Sub