Skip to content

Instantly share code, notes, and snippets.

@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
@danwagnerco
danwagnerco / medianifs.vb
Last active March 3, 2018 08:07
This UDF (user-defined function) implements MEDIANIFS, which acts like SUMIFS or AVERAGEIFS but calculates the median instead
Option Explicit
Public Function MEDIANIFS(median_range As Range, ParamArray range_and_criteria_pairs())
Dim lngIdx As Long, lngMedianRowIdx As Long, lngCriteriaIdx As Long
Dim strOperator As String
Dim varThreshold As Variant, varAccumulator() As Variant
ReDim varAccumulator(0)
Dim blnAllMatched As Boolean
'''''''''''''''
@danwagnerco
danwagnerco / convert_semicolon_text_to_xls.vb
Created April 20, 2016 18:45
Prompt user to select all the semicolon-delimited files they would like to convert into XLS files
Option Explicit
Public Sub ConvertSemicolonTextToXLS()
Dim fdoUserPicks As FileDialog
Dim strMessage As String, strFilename As String
Dim wbkData As Workbook
Dim lngIdx As Long
'Leverage the already-written-for-you PromptUserToSelectFiles
'function from the VBA Toolbelt to prompt the user to select files
Option Explicit
Public Sub CombineDataFromAllSheets()
Dim wksSrc As Worksheet, wksDst As Worksheet
Dim rngSrc As Range, rngDst As Range
Dim lngLastCol As Long, lngSrcLastRow As Long, lngDstLastRow As Long
'Notes: "Src" is short for "Source", "Dst" is short for "Destination"
'Set references up-front