Skip to content

Instantly share code, notes, and snippets.

@danwagnerco
danwagnerco / toolbelt_release_checklist.md
Last active September 20, 2015 17:50
Checklist for toolbelt release
  • Copy A_Functions to a new workbook
  • Copy C_Pearson to the same workbook
  • Add the following references:
    • Microsoft Scripting Runtime
    • Microsoft VBScript Regular Expressions 5.5
    • Windows Script Host Object Model
  • Change VBAProject to toolbelt
  • Set version in Project Properties > Project Description
  • Put the file in Dropbox > toolbelt
  • Email subscribers to let them know about the update
@danwagnerco
danwagnerco / run_batch_file.vb
Last active September 20, 2015 02:02
This script runs a batch file from VBA using the WshShell object
Option Explicit
Public Sub RunBatchFile()
Dim strCommand As String
Dim lngErrorCode As Long
Dim wsh As WshShell
Set wsh = New WshShell
'Run the batch file using the WshShell object
strCommand = Chr(34) & _
@danwagnerco
danwagnerco / run_batch_file_then_rename_resulting_folder.vb
Last active September 20, 2015 02:20
This script runs a batch file, but waits for that the batch file to finish before moving on (and eventually modifying the results of the batch file)
Option Explicit
Public Sub RunBatchFileThenRenameFolder()
Dim strNewFolderName As String, strRunBatchCommand As String, _
strRenameFolderCommand As String
Dim lngErrorCode As Long
Dim wks As Worksheet
Dim wsh As WshShell
Set wsh = New WshShell
@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 / convert_lookups_to_values.vb
Created January 21, 2016 04:31
This macro converts successful VLOOKUP formulas into values (thereby "locking" them) in case your lookup table has to change
Option Explicit
Public Sub ConvertLookupsToValues()
Dim lngLastRow As Long, lngIdx As Long
Dim varLookups As Variant
'Everything happens on the Ledger sheet
With ThisWorkbook.Worksheets("Ledger")
'Identify the last-occupied row in column D on the Ledger sheet,
@danwagnerco
danwagnerco / delete_rows_with_for_loop.vb
Last active January 22, 2016 13:00
This script deletes rows that match "Invoice.zip" or "Thumbs.db"
Option Explicit
Public Sub DeleteRowsWithForLoop()
Dim wksData As Worksheet
Dim lngLastRow As Long, lngIdx As Long
'Set references up-front
Set wksData = ThisWorkbook.Worksheets("data")
With wksData
@danwagnerco
danwagnerco / delete_rows_with_autofilter.vb
Last active January 28, 2016 20:37
This macro deletes rows using the Range.AutoFilter method (instead of a For loop)
Option Explicit
Public Sub DeleteRowsWithAutofilter()
Dim wksData As Worksheet
Dim lngLastRow As Long
Dim rngData As Range
'Set references up-front
Set wksData = ThisWorkbook.Worksheets("data")
@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 / 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 / write_uniques_to_new_sheet.vb
Last active March 11, 2016 03:41
This script prompts the user to select a range, then puts all the unique values from that range into a single column list on a new worksheet
Public Sub WriteUniquesToNewSheet()
Dim wksUniques As Worksheet
Dim rngUniques As Range, rngTarget As Range
Dim strPrompt As String
Dim varUniques As Variant
Dim lngIdx As Long
Dim colUniques As Collection
Set colUniques = New Collection