Skip to content

Instantly share code, notes, and snippets.

Dim variable as SomeObjectType
Set variable = New SomeObjectType
@danwagnerco
danwagnerco / exampleLateBinding.vb
Created October 2, 2016 21:28
an example of late-binding in VBA
Dim variable as Object
Set variable = CreateObject("SomeObjectType")
@danwagnerco
danwagnerco / TestCreateGUID.vb
Last active October 2, 2016 23:05
This tiny subroutine walks through all of the options available in the CreateGUID function
Option Explicit
Public Sub TestCreateGUID()
'Dim t As Scriptlet.TypeLib <~ error, user type not defined
'Dim t As Scriptlet.IGenScriptletTLib <~ at least compiles
'Debug.Print t.Name <~ error, object or with block variable not set
' i.e. doesn't understand t as a variable
'Debug.Print t.GUID <~ same
'Debug.Print t.AnyOtherListedMethodOnThisObject
@danwagnerco
danwagnerco / CreateGUID.vb
Last active April 18, 2019 23:54
This function returns a GUID (globally unique identifier) primarily for use in VBA scripts
Option Explicit
Public Function CreateGUID(Optional IncludeHyphens As Boolean = True, _
Optional IncludeBraces As Boolean = False) _
As String
Dim obj As Object
Dim strGUID As String
'Late-bind obj as a TypeLib -- a rare time when late-binding
'is actually a must!
@danwagnerco
danwagnerco / combine_sheets_with_different_headers.vb
Created July 8, 2016 04:44
This script combines many sheets into a single sheet even when the columns on each sheet are different (or are in different order)
Option Explicit
Public Sub CombineSheetsWithDifferentHeaders()
Dim wksDst As Worksheet, wksSrc As Worksheet
Dim lngIdx As Long, lngLastSrcColNum As Long, _
lngFinalHeadersCounter As Long, lngFinalHeadersSize As Long, _
lngLastSrcRowNum As Long, lngLastDstRowNum As Long
Dim strColHeader As String
Dim varColHeader As Variant
Dim rngDst As Range, rngSrc As Range
@danwagnerco
danwagnerco / test_contains.vb
Created July 1, 2016 05:48
This short script tests a variety of Collections (using the Contains function)
Public Sub TestContains()
'Test "primitives" collection (i.e. Strings, Longs, etc.)
Dim colStrings As Collection
Set colStrings = New Collection
'Add Item / Key pairs (of strings)
colStrings.Add Item:="Item1", Key:="Key1"
colStrings.Add Item:="Item2", Key:="Key2"
@danwagnerco
danwagnerco / contains.vb
Last active July 1, 2016 10:51
A bulletproof VBA function that examines a Collection for a Key or Item
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'INPUT : Kollection, the collection we would like to examine
' : (Optional) Key, the Key we want to find in the collection
' : (Optional) Item, the Item we want to find in the collection
'OUTPUT : True if Key or Item is found, False if not
'SPECIAL CASE: If both Key and Item are missing, return False
Option Explicit
Public Function Contains(Kollection As Collection, Optional Key As Variant, Optional Item As Variant) As Boolean
Dim strKey As String
Dim var As Variant
@danwagnerco
danwagnerco / fast_mark_duplicates_with_dictionary.vb
Last active June 24, 2016 15:03
A speedy way to identify and label items that only occur once as "unique" using VBA in Excel
Option Explicit
Public Sub FastMarkDuplicatesWithDictionary()
'For timing purposes only -- this does not affect our macro!
Dim dblStart As Double
dblStart = Timer
Dim wksIDs As Worksheet
Dim varIDs As Variant, varStatus As Variant, _
varID As Variant
@danwagnerco
danwagnerco / insert_new_rows_based_on_values.vb
Last active February 16, 2021 00:24
This script loops BACKWARDS through a block of data, applying logic and noting which rows mandate a new row insert. It then loops through the collected "insert" rows, applying the final logic
Option Explicit
Public Sub InsertNewRowsBasedOnValues()
Dim wksData As Worksheet
Dim lngLastRow As Long, lngIdx As Long, _
lngStudentCol As Long, lngItemCol As Long, lngNetAmtCol As Long, _
lngPreviousAmtCol As Long, lngNewAmtCol As Long, _
lngReversalCol As Long
Dim varRowNum As Variant
Dim colRowNumsForInsert As Collection
@danwagnerco
danwagnerco / combine_certain_sheets.vb
Created May 27, 2016 10:27
This script initializes the destination sheet then combines data from certain sheets (and not others)
Option Explicit
Public Sub CombineCertainSheets()
Dim wks As Worksheet, wksDst As Worksheet
Dim strName As String
Dim lngSrcLastRow As Long, lngDstLastRow As Long, _
lngLastCol As Long
Dim rngSrc As Range, rngDst As Range, rngToClear As Range
'Set references up-front