Skip to content

Instantly share code, notes, and snippets.

@danwagnerco
danwagnerco / combine_data_and_transpose_results_to_rows.vb
Last active August 29, 2015 14:22
This script combines data but writes the results to rows (with a new column for each item)
Option Explicit
Public Sub SplitCampersByCabin()
Dim lngLastRow As Long, lngIdx As Long, lngNextCol As Long, _
lngCabinRow As Long
Dim wksCampers As Worksheet, wksCabins As Worksheet
Dim varCamper As Variant, varCabin As Variant
Dim dicCampers As Scripting.Dictionary, _
dicCabins As Scripting.Dictionary
Dim strCamper As String, strCabin As String
@danwagnerco
danwagnerco / delete_blank_rows_based_on_a_varying_range.vb
Last active August 29, 2015 14:23
This script examines a dynamic range and deletes empty rows (based on the range size)
Option Explicit
Public Sub DeleteBlankRows()
Dim wks As Worksheet
Dim lngLastRow As Long, lngLastCol As Long, lngIdx As Long, _
lngColCounter As Long
Dim blnAllBlank As Boolean
'First things first: we identify our basic variables
Set wks = ThisWorkbook.Worksheets("hello")
@danwagnerco
danwagnerco / twister_spinner.rb
Created July 31, 2015 02:16
Short reproduction of a friend's twister emulator
def spinner
colors = ["Red", "Blue", "Yellow", "Green"]
appendages = ["Left Hand", "Right Hand", "Left Foot", "Right Foot"]
puts "Place your #{appendages.sample} on #{colors.sample}!"
end
spinner
@danwagnerco
danwagnerco / slowly_deleting_rows.vb
Last active August 29, 2015 14:27
This For loop deletes rows correctly (but slowly)
Option Explicit
Public Sub DeleteRowsSlowly()
Dim lngIdx As Long
For lngIdx = 100000 To 1 Step -1
If Cells(lngIdx, 1).Value = vbNullString Then
Cells(lngIdx, 1).EntireRow.Delete
End If
Next lngIdx
End Sub
@danwagnerco
danwagnerco / slowly_deleting_rows_that_are_less_than_50.vb
Last active August 29, 2015 14:27
This For loop deletes rows where the cell value is less than 50 (but slowly)
Option Explicit
Public Sub DeleteRowsLessThanFiftySlowly()
Dim lngIdx As Long
For lngIdx = 100000 To 1 Step -1
If Cells(lngIdx, 1).Value < 50 Then
Cells(lngIdx, 1).EntireRow.Delete
End If
Next lngIdx
End Sub
@danwagnerco
danwagnerco / slowly_deleting_rows_that_are_older_than_a_date.vb
Last active August 29, 2015 14:27
This For loop deletes rows where the cell value is more recent than February 1st, 2013 (but slowly)
Option Explicit
Public Sub DeleteDatesMoreRecentThanFebFirstSlowly()
Dim lngIdx As Long
For lngIdx = 1000000 To 1 Step -1
If Cells(lngIdx, 1).Value > DateValue("2/1/2013") Then
Cells(lngIdx, 1).EntireRow.Delete
End If
Next lngIdx
End Sub
Option Explicit
Public Sub ExtractInfoFromSquareBrackets()
Dim wksRaw As Worksheet
Dim strPattern As String, strRaw As String, strMatch As String
Dim rngAllRows As Range, rngCell As Range
Dim lngLastRow As Long, lngIdx As Long
Dim objMatches As Object
Dim rgx As RegExp
Set rgx = New RegExp
@danwagnerco
danwagnerco / save_sheets_as_pdf.vb
Last active September 4, 2015 13:46
This short script creates a single PDF from a three-sheet Workbook
Option Explicit
Public Sub SaveSheetsAsPDF()
Dim wksAllSheets As Variant
Dim wksSheet1 As Worksheet
Dim strFilename As String, strFilepath As String
'Set references up-front
Set wksSheet1 = ThisWorkbook.Sheets("Sheet1")
wksAllSheets = Array("Sheet1", "Sheet2", "Sheet3")
@danwagnerco
danwagnerco / move_data_based_on_drop_down.vb
Last active September 6, 2015 11:50
This script moves data on the 'Allocate' sheet to a dynamic destination sheet based on user input
Option Explicit
Public Sub MoveDataBasedOnDropDown()
Dim strInput As String, strPromptMessage As String
Dim wksAllocate As Worksheet, wksTarget As Worksheet
Dim obj As Object
Dim lngAllocateLastRow As Long, lngAllocateLastCol As Long, _
lngTargetLastRow As Long
Dim rngAllocate As Range, rngTarget As Range
@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) & _