Skip to content

Instantly share code, notes, and snippets.

@danwagnerco
danwagnerco / transpose_horizontal_data_to_vertical.vb
Last active August 27, 2015 03:20
This macro "de-pivots" a few columns (and repeats the corresponding rows) to make forming a pivot table easy
Option Explicit
Public Sub TransposeHorizontalToVertical()
Dim lngLastRow As Long, lngIdx As Long, lngOutputLastRow As Long, _
lngDetailsIdx As Long, lngTargetRow As Long, lngTargetCol As Long
Dim wksInput As Worksheet, wksOutput As Worksheet
Dim varDetailNames As Variant, varMonthNames As Variant, _
varDetails As Variant, varValues As Variant
Dim varDetailsKey As Variant, varValuesKey As Variant
Dim dicDetails As Scripting.Dictionary, dicValues As Scripting.Dictionary
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 / delete_rows_fast_with_autofilter.vb
Last active November 29, 2022 07:09
This short script deletes rows IN A HURRY by leveraging Range.Autofilter
Option Explicit
Public Sub DeleteRowsFastWithAutofilter()
Dim wksData As Worksheet
Dim rngDataBlock As Range
Dim lngLastRow As Long, lngLastCol As Long
'Set references up-front
Set wksData = ThisWorkbook.Sheets("data")
@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
@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.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 / 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 / import_all_data_files.vb
Last active June 8, 2016 16:30
This 3-part script collects data from CSV files, stores it in a master file and moves the already-imported CSVs into a "processed" folder
Option Explicit
Public Sub ImportAllDataFiles()
Dim strFile As String
Dim wbk As Workbook
Dim wks As Worksheet, wksMaster As Worksheet
Dim lngLastMasterRow As Long, lngFirstMasterRow As Long, _
lngLastDataRow As Long, lngLastDataCol As Long, lngIdx As Long, _
lngTimeZoneCol As Long, lngDateCol As Long
Dim rngTimeZoneCol As Range, rngDateCol As Range, _
@danwagnerco
danwagnerco / vim_vundle_and_conemu.md
Created June 22, 2015 20:43
This is a Windows-specific walkthrough for installing Vim, Vundle and ConEmu

Vim, Vundle and ConEmu on Windows

Let's start by getting it out on the table: Sublime Text is great, and version 3 should no longer be considered "abandonware" as of build 3065. Sublime Text served me very, very well over the years. That said, trying out new things is a major part of leveling-up, and in that vein Vim deserves a go. (And have you seen some of those thoughtbot guys flying around in Vim? It's awesome!)

Getting Vim up-and-running on your Windows machine doesn't have to be an all-day project. In this post, we'll walk through:

  • Installing gVim, which gives us both the "classic" command line version as well as the graphical version
  • Installing Vundle, the best way to handle Vim-enhancing packages
  • Installing ConEmu, a supercharged command line emulator for Windows
@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")