Skip to content

Instantly share code, notes, and snippets.

@danwagnerco
danwagnerco / zip_folders_in_column.vb
Last active January 15, 2021 20:52
This script loops through a column and creates zip files for each folder listed using 7-Zip
Option Explicit
Public Sub ZipFoldersInColumn()
Dim wks As Worksheet
Dim lngIdx As Long, lngErrorCode
Dim strFullPath As String, strZipName As String, strEndFound As String, _
strCommand As String, strTargetDir As String
Dim blnEndFound As Boolean
Dim wsh As WshShell
Set wsh = New WshShell
@danwagnerco
danwagnerco / collect_uniques.vb
Last active August 13, 2020 16:56
This function walks through a Range and returns a Collection of the unique values
Public Function CollectUniques(rng As Range) As Collection
Dim varArray As Variant, var As Variant
Dim col As Collection
'Guard clause - if Range is nothing, return a Nothing collection
'Guard clause - if Range is empty, return a Nothing collection
If rng Is Nothing Or WorksheetFunction.CountA(rng) = 0 Then
Set CollectUniques = col
Exit Function
@danwagnerco
danwagnerco / combine_data_from_all_sheets.vb
Last active August 7, 2020 12:58
This macro combines data from many sheets into a single sheet
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
@danwagnerco
danwagnerco / create_subset_workbook_based_on_dates.vb
Last active July 5, 2020 17:12
This script creates a new workbook containing ONLY data between the input dates
Option Explicit
'This subroutine prompts the user to select dates
Public Sub PromptUserForInputDates()
Dim strStart As String, strEnd As String, strPromptMessage As String
'Prompt the user to input the start date
strStart = InputBox("Please enter the start date")
@danwagnerco
danwagnerco / CombineManyWorkbooksIntoOneWorksheet.vb
Last active May 16, 2019 16:51
This VBA macro combines data from many workbooks in a single folder into a single worksheet
Option Explicit
Public Sub CombineManyWorkbooksIntoOneWorksheet()
Dim strDirContainingFiles As String, strFile As String, _
strFilePath As String
Dim wbkDst As Workbook, wbkSrc As Workbook
Dim wksDst As Worksheet, wksSrc As Worksheet
Dim lngIdx As Long, lngSrcLastRow As Long, _
lngSrcLastCol As Long, lngDstLastRow As Long, _
lngDstLastCol As Long, lngDstFirstFileRow As Long
@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 / create_a_new_column_based_on_a_different_column.vb
Created June 5, 2015 11:57
Adding calculated columns FAST using variant arrays
Option Explicit
Public Sub CalculateResultsAndAddAsColumn()
Dim rngCategory As Range, rngResults As Range
Dim varCategory As Variant, varResults As Variant
Dim lngIdx As Long, lngLastRow As Long
Dim wksData As Worksheet
Dim strFirstLetter As String
'First things first: let's set up our basic variables
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'INPUT : Sheet, the worksheet we'll search to find the last column
' : RowNum, the row (as a number) we're interested in
'OUTPUT : Long, the last occupied column in the specified row
'SPECIAL CASE : if a bad row number is entered (anything < 1), return 0
'EXAMPLES BELOW:
'
'assume that on MySheet, cells A1:F1 are occupied,
'but B3 is the only occupied cell in row 3
'LastColNumInRow(MySheet, 3)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'INPUT : Sheet, the worksheet we'll search to find the last column
'OUTPUT : Long, the last occupied column
'SPECIAL CASE : if Sheet is empty, return 1
'EXAMPLES BELOW:
'
'assume that there is a single entry on MySheet in cell C3
'LastColNum(MySheet)
'>> 3
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'INPUT : True or False (i.e. fast or slow)
'DESCRIPTION : this sub turns off screen updating and alarms then
' sets calculations to manual
'
Public Sub GoFast(OnOrOff As Boolean)
Dim CalcMode As XlCalculation
CalcMode = Application.Calculation
With Application
.ScreenUpdating = Not OnOrOff