Skip to content

Instantly share code, notes, and snippets.

@lundeen-bryan
Last active March 18, 2023 23:56
Show Gist options
  • Save lundeen-bryan/d7c76a5476ef47b8f1b700cdea526b85 to your computer and use it in GitHub Desktop.
Save lundeen-bryan/d7c76a5476ef47b8f1b700cdea526b85 to your computer and use it in GitHub Desktop.
vba snippets

Option Explicit

'used to get last row of a column Function last_row_in_column(column_letter As String) As String Dim data_in_column As Range, result As Long Set data_in_column = Range(column_letter & "1:" & _ column_letter & _ Cells(Rows.Count, column_letter).End(xlUp).Row) Let result = data_in_column.Find("*", , xlValues, , xlByRows, xlPrevious).Row Let last_row_in_column = result End Function

{
"AssignBoolean": {
"description": "Insert a boolean variable with inline assignment",
"prefix": "vbaDimbass",
"body": [
"Dim ${1:bool_var_name} as boolean: ${1:bool_var_name} = ${2:value}\n$0"
]
},
"AssignInteger": {
"description": "Insert an integer variable with inline assignment",
"prefix": "vbaDimiass",
"body": [
"Dim ${1:int_var_name} as Integer: ${1:int_var_name} = ${2:value}\n$0"
]
},
"AssignString": {
"description": "Insert a string variable with inline assignment",
"prefix": "vbaDimsass",
"body": [
"Dim ${1:int_var_name} as String: ${1:int_var_name} = ${2:value}\n$0"
]
},
"AssignType": {
"description": "Insert a variable with inline assignment",
"prefix": "vbaDimtass",
"body": [
"Dim ${1:var_name} as ${2:type}: ${1:var_name} = ${3:value}\n$0"
]
},
"Boolean": {
"description": "Insert a boolean variable",
"prefix": "vbaDimb",
"body": [
"Dim ${1:bool_var_name} as boolean\n$0"
]
},
"Call": {
"description": "Call",
"prefix": "vbaCall",
"body": [
"Call"
]
},
"Cells": {
"description": "Cells",
"prefix": "vbaCells",
"body": [
"Cells($1)"
]
},
"Clear": {
"description": "Clear",
"prefix": "vbaClear",
"body": [
"Clear"
]
},
"ClearContents": {
"description": "ClearContents",
"prefix": "vbaClearContents",
"body": [
"ClearContents"
]
},
"Columns": {
"description": "Columns",
"prefix": "vbaColumns",
"body": [
"Columns"
]
},
"CommentAfterLine": {
"description": "Insert comment after line",
"prefix": "vbaCommentAfter",
"body": [
"",
"'^--${1:comment}"
]
},
"CommentSection": {
"description": "Insert comment block",
"prefix": "vbaCommentSection",
"body": [
"'=======================================================",
"' ${1:commenthere}",
"'======================================================="
]
},
"CreateObject": {
"description": "CreateObject",
"prefix": "vbaCreateObject",
"body": [
"CreateObject"
]
},
"Date": {
"description": "Insert a date variable",
"prefix": "vbaDimd",
"body": [
"Dim ${1:date_var_name} as Date\n$0"
]
},
"DoLoopWhile": {
"description": "Do Loop While code block",
"prefix": "vbaDoLoopWhile",
"body": [
"Do",
"\t$0",
"Loop While ${1:condition}"
]
},
"DoWhile": {
"description": "Do While Loop code block",
"prefix": "vbaDoWhile",
"body": [
"Do While ${1:condition}",
"\t$0",
"Loop"
]
},
"Else": {
"description": "Else",
"prefix": "vbaElse",
"body": [
"Else",
"\t$0"
]
},
"ElseIf": {
"description": "ElseIf",
"prefix": "vbaElseIf",
"body": [
"ElseIf ${1:condition2} Then",
"\t$0"
]
},
"End": {
"description": "End",
"prefix": "End",
"body": [
"End"
]
},
"Explicit": {
"description": "Adds option explicit",
"prefix": "vbaOption",
"body": [
"Option Explicit\n$0"
]
},
"Fix": {
"description": "Fix",
"prefix": "vbaFix",
"body": [
"Fix($1)"
]
},
"Footer": {
"description": "Place footer at end of module with desciption text.",
"prefix": "vbaFooter",
"body": [
"\nProcedureFooter:",
"$LINE_COMMENT$LINE_COMMENT===========================================================================================",
"$LINE_COMMENT$LINE_COMMENT Procedure: ......... $TM_FILENAME/${1:Procedurename}",
"$LINE_COMMENT$LINE_COMMENT Description: ....... ${2:description}",
"$LINE_COMMENT$LINE_COMMENT Version: ........... 1.0.0 - major.minor.patch",
"$LINE_COMMENT$LINE_COMMENT Created: ........... ${3:$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE}",
"$LINE_COMMENT$LINE_COMMENT Updated: ........... ${4:$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE}",
"$LINE_COMMENT$LINE_COMMENT Module URL: ........ ${5:weburl}",
"$LINE_COMMENT$LINE_COMMENT Installs to: ....... ${TM_DIRECTORY/.*\\\\(.*)\\\\(.*)$/$1\\/$2/}",
"$LINE_COMMENT$LINE_COMMENT Compatibility: ..... ${6:Excel,Word,etc.}",
"$LINE_COMMENT$LINE_COMMENT Contact Author: .... ${7:author}",
"$LINE_COMMENT$LINE_COMMENT Copyright: ........ ${8:company} ©$CURRENT_YEAR. All rights reserved.",
"$LINE_COMMENT$LINE_COMMENT Called by: ......... ${9:other_subs}",
"$LINE_COMMENT$LINE_COMMENT Calls to: .......... ${10:other_subs}",
"$LINE_COMMENT$LINE_COMMENT Parameters: ........ ${11:parameters}",
"$LINE_COMMENT$LINE_COMMENT Return: ............ ${12:type} ${13:param_description}",
"$LINE_COMMENT$LINE_COMMENT Notes: ............. _",
" (1) ${14:notes_here}",
"$LINE_COMMENT$LINE_COMMENT===========================================================================================$0"
]
},
"For Loop": {
"description": "For Next Loop",
"prefix": "vbaFor",
"body": [
"For ${1:i} = ${3:lb} To ${4:ub} Step ${5:1}",
"\t$0",
"Next ${1:i}"
]
},
"ForEach": {
"description": "For Each",
"prefix": "vbaForEach",
"body": [
"For Each ${1:variable} In ${2:collection}",
"\t$0",
"Next ${1:variable}"
]
},
"Function": {
"description": "Function code block",
"prefix": "vbaFunc",
"body": [
"Function ${1:func}_fnc(ByVal ${2:params}) as ${3:return_datatype}",
"'",
"' Desc: ....... ${4:description}.",
"'",
"' Version: .... ${5:1.0.0}",
"' Params: ..... {${6:datatype}} [${7:name}] ${8:description}.",
"' Returns: .... {${9:datatype}} [${10:name}] ${11:description}.",
"' Call to: .... ${12:dependencies}",
"'",
"",
"\t$0",
"",
"End Function"
]
},
"Function With Err": {
"description": "Function with err handling",
"prefix": "vbaFuncerr",
"body": [
"Function ${1:function_name}_fnc(ByVal ${2:params}) as ${3:return_datatype}",
"'",
"' Desc: ....... ${4:description}.",
"'",
"' Version: .... ${5:1.0.0}",
"' Params: ..... {${6:datatype}} [${7:name}] ${8:description}.",
"' Returns: .... {${9:datatype}} [${10:name}] ${11:description}.",
"' Call to: .... ${12:dependencies}",
"'",
"",
"\ton error goto ErrorHandler\n",
"\t$4\n",
"\t${1:function_name} = ${3:value}",
"\ton error goto 0",
"\texit sub",
"ErrorHandler:",
"\t$0",
"",
"End Function"
]
},
"If": {
"description": "If code block",
"prefix": "vbaIf",
"body": [
"If ${1:condition} Then",
"\t$2",
"End If"
]
},
"Int": {
"description": "Int",
"prefix": "vbaInt",
"body": [
"Int($1)"
]
},
"Integer": {
"description": "Insert an Integer variable",
"prefix": "vbaDimi",
"body": [
"Dim ${1:int_var_name} as Integer\n$0"
]
},
"IsEmpty": {
"description": "IsEmpty",
"prefix": "vbaIsEmpty",
"body": [
"IsEmpty($1)"
]
},
"Item": {
"description": "Item",
"prefix": "Item",
"body": [
"Item($1)"
]
},
"LBound": {
"description": "LBound",
"prefix": "vbaLBound",
"body": [
"LBound($1)"
]
},
"Left": {
"description": "Left",
"prefix": "Left",
"body": [
"Left($1)"
]
},
"Len": {
"description": "Len",
"prefix": "Len",
"body": [
"Len($1)"
]
},
"Let Statement": {
"body": "Let ${1:varName} = ${2:varValue}",
"description": "[Depricated] Insert Let statement and value",
"prefix": "vbaLet"
},
"Max": {
"description": "Max",
"prefix": "Max",
"body": [
"Max($1)"
]
},
"Min": {
"description": "Min",
"prefix": "Min",
"body": [
"Min($1)"
]
},
"MsgBox": {
"description": "Message box code block",
"prefix": "vbaMsgbox",
"body": [
"MsgBox \"${1:message}\", ${2:buttonType}, \"${3:title}\""
]
},
"On Error Resume Next": {
"description": "On Error Resume Next",
"prefix": "vbaOnError",
"body": [
"On Error Resume Next"
]
},
"Option Explicit": {
"description": "Option Explicit",
"prefix": "vbaOption",
"body": [
"Option Explicit"
]
},
"Preserve": {
"description": "Preserve",
"prefix": "vbaPreserve",
"body": [
"Preserve"
]
},
"Print": {
"description": "Log variable value to console",
"prefix": "vbaPrint",
"body": [
"Debug.Print ${1:var_name}"
]
},
"PrintString": {
"description": "Prints explicit String to console",
"prefix": "vbaPrints",
"body": [
"Debug.Print \"$1\""
]
},
"Private Function": {
"description": "Function code block",
"prefix": "vbaFuncpvt",
"body": [
"Private Function ${1:func}_fnc(ByVal ${2:params}) as ${3:return_datatype}",
"'",
"' Desc: ....... ${4:description}.",
"'",
"' Version: .... ${5:1.0.0}",
"' Params: ..... {${6:datatype}} [${7:name}] ${8:description}.",
"' Returns: .... {${9:datatype}} [${10:name}] ${11:description}.",
"' Call to: .... ${12:dependencies}",
"'",
"",
"\t$0",
"",
"End Function"
]
},
"Private Function With Err": {
"description": "Private Function with err handling",
"prefix": "vbaFuncerrpvt",
"body": [
"Function ${1:function_name}_fnc(ByVal ${2:params}) as ${3:return_datatype}",
"'",
"' Desc: ....... ${4:description}.",
"'",
"' Version: .... ${5:1.0.0}",
"' Params: ..... {${6:datatype}} [${7:name}] ${8:description}.",
"' Returns: .... {${9:datatype}} [${10:name}] ${11:description}.",
"' Call to: .... ${12:dependencies}",
"'",
"",
"\ton error goto ErrorHandler\n",
"\t$4\n",
"\t${1:function_name} = ${3:value}",
"\ton error goto 0",
"\texit sub",
"ErrorHandler:",
"\t$0",
"",
"End Function"
]
},
"PrivateBoolean": {
"description": "Insert a Private boolean variable",
"prefix": "vbaDimbpvt",
"body": [
"Private ${1:bool_var_name} As Boolean\n$0"
]
},
"PrivateInteger": {
"description": "Insert a Private integer variable",
"prefix": "vbaDimipvt",
"body": [
"Private ${1:int_var_name} as Integer\n$0"
]
},
"PrivateString": {
"description": "Insert a Private string variable",
"prefix": "vbaDimspvt",
"body": [
"Private ${1:int_var_name} as String\n$0"
]
},
"PrivateType": {
"description": "Insert a Private variable",
"prefix": "vbaDimtpvt",
"body": [
"Private ${1:var_name} as ${2:type}\n$0"
]
},
"Public Function": {
"description": "Function code block",
"prefix": "vbaFuncpub",
"body": [
"Public Function ${1:function_name}_fnc(ByVal ${2:params}) as ${3:return_datatype}",
"'",
"' Desc: ....... ${4:description}.",
"'",
"' Version: .... ${5:1.0.0}",
"' Params: ..... {${6:datatype}} [${7:name}] ${8:description}.",
"' Returns: .... {${9:datatype}} [${10:name}] ${11:description}.",
"' Call to: .... ${12:dependencies}",
"'",
"",
"\t$0",
"",
"End Function"
]
},
"Public Function With Err": {
"description": "Public Function with err handling",
"prefix": "vbaFuncerrpub",
"body": [
"Public Function ${1:function_name}_fnc(ByVal ${2:params}) as ${3:return_datatype}",
"'",
"' Desc: ....... ${4:description}.",
"'",
"' Version: .... ${5:1.0.0}",
"' Params: ..... {${6:datatype}} [${7:name}] ${8:description}.",
"' Returns: .... {${9:datatype}} [${10:name}] ${11:description}.",
"' Call to: .... ${12:dependencies}",
"'",
"",
"\ton error goto ErrorHandler\n",
"\t$4\n",
"\t${1:function_name} = ${3:value}",
"\ton error goto 0",
"\texit sub",
"ErrorHandler:",
"\t$0",
"",
"End Function"
]
},
"PublicBoolean": {
"description": "Insert a public boolean variable",
"prefix": "vbaDimbpub",
"body": [
"public ${1:bool_var_name} as boolean\n$0"
]
},
"PublicInteger": {
"description": "Insert a public integer variable",
"prefix": "vbaDimipub",
"body": [
"public ${1:int_var_name} as Integer\n$0"
]
},
"PublicString": {
"description": "Insert a public string variable",
"prefix": "vbaDimspub",
"body": [
"public ${1:int_var_name} as String\n$0"
]
},
"PublicSubroutine": {
"description": "Creates a public subroutine",
"prefix": "vbaSubpub",
"body": [
"Public Sub ${1:subroutine_name}_sub()",
"'",
"' Desc: ....... ${2:description}.",
"'",
"' Version: .... ${3:1.0.0}",
"' Call to: .... ${4:dependencies}",
"'",
"",
"\t$0",
"End Sub"
]
},
"PublicSubroutineCatch": {
"description": "Creates a public subroutine with a try-catch inside",
"prefix": "vbaSubpuber",
"body": [
"Public Sub ${1:subroutine_name}_sub()",
"'",
"' Desc: ....... ${2:description}.",
"'",
"' Version: .... ${3:1.0.0}",
"' Call to: .... ${4:dependencies}",
"'",
"",
"\ton error goto ErrorHandler\n",
"\t$2\n",
"\ton error goto 0",
"\texit sub",
"ErrorHandler:",
"\t$0",
"End Sub"
]
},
"PublicType": {
"description": "Insert a public variable",
"prefix": "vbaDimtpub",
"body": [
"public ${1:var_name} as ${2:type}\n$0"
]
},
"Range": {
"description": "Range",
"prefix": "vbaRange",
"body": [
".Range(.Cells($1), .Cells($2))"
]
},
"ReDim": {
"description": "ReDim",
"prefix": "vbaReDim",
"body": [
"ReDim"
]
},
"Right": {
"description": "Right",
"prefix": "Right",
"body": [
"Right($1)"
]
},
"Round": {
"description": "Round",
"prefix": "Round",
"body": [
"Round($1)"
]
},
"RoundUp": {
"description": "RoundUp",
"prefix": "RoundUp",
"body": [
"RoundUp($1)"
]
},
"Rows": {
"description": "Rows",
"prefix": "vbaRows",
"body": [
"Rows"
]
},
"SelectCase": {
"description": "Select Case code block",
"prefix": "vbaSelectCase",
"body": [
"Select Case ${1:test}",
"",
"\tCase ${2:lists}",
"",
"\t\t${3:statements}",
"",
"\tCase Else",
"",
"\t\t${0:elseStatement}",
"",
"End Select"
]
},
"Set": {
"description": "Set",
"prefix": "vbaSet",
"body": [
"Set"
]
},
"SetupAttribute": {
"description": "Sets up mod with Attribute etc.",
"prefix": "vbaSetup",
"body": [
"Attribute VB_Name = \"${1:fncOrsub}_${2:module_name}\"",
"$LINE_COMMENT Namespace=${TM_DIRECTORY/.*\\\\(.*)\\\\(.*)$/$1\\/$2/}",
"$LINE_COMMENT Filename=$TM_FILENAME",
"$LINE_COMMENT !See Label ProcedureFooter for notes",
"Option Explicit",
""
]
},
"Split": {
"description": "Split",
"prefix": "vbaSplit",
"body": [
"Split(${1:string}, \"${2:delimiter}\")"
]
},
"Sqr": {
"description": "Sqr",
"prefix": "Sqr",
"body": [
"Sqr($1)"
]
},
"String": {
"description": "Insert a string variable",
"prefix": "vbaDims",
"body": [
"Dim ${1:int_var_name} as String\n$0"
]
},
"Subroutine": {
"description": "Creates a Private subroutine",
"prefix": "vbaSub",
"body": [
"Sub ${1:subroutine_name}_sub()",
"'",
"' Desc: ....... ${2:description}.",
"'",
"' Version: .... ${3:1.0.0}",
"' Call to: .... ${4:dependencies}",
"'",
"",
"\t$0",
"End Sub"
]
},
"SubroutineCatch": {
"description": "Creates a Private subroutine with a try-catch inside",
"prefix": "vbaSuber",
"body": [
"Private Sub ${1:subroutine_name}_sub()",
"'",
"' Desc: ....... ${2:description}.",
"'",
"' Version: .... ${3:1.0.0}",
"' Call to: .... ${4:dependencies}",
"'",
"",
"\ton error goto ErrorHandler\n",
"\t$2\n",
"\ton error goto 0",
"\texit sub",
"ErrorHandler:",
"\t$0",
"End Sub"
]
},
"SubroutinePrivate": {
"description": "Creates a Private subroutine",
"prefix": "vbaSubpvt",
"body": [
"Private Sub ${1:subroutine_name}_sub()",
"'",
"' Desc: ....... ${2:description}.",
"'",
"' Version: .... ${3:1.0.0}",
"' Call to: .... ${4:dependencies}",
"'",
"",
"\t$0",
"End Sub"
]
},
"TestSubroutine": {
"description": "Creates a Private subroutine",
"prefix": "vbaSubTest",
"body": [
"Sub test_${1:subroutine_name}()",
"'",
"",
"Debug.Print \"\"",
"\t$0",
"End Sub"
]
},
"To": {
"description": "To",
"prefix": "vbaTo",
"body": [
"To"
]
},
"Type": {
"description": "Insert a variable",
"prefix": "vbaDimt",
"body": [
"Dim ${1:var_name} as ${2:type}\n$0"
]
},
"UBound": {
"description": "UBound",
"prefix": "vbaUBound",
"body": [
"UBound($1)"
]
},
"Update Code": {
"description": "When updates are created add this snippet.",
"prefix": "vbaUpdateCode",
"body": [
"$LINE_COMMENT$LINE_COMMENT Updated: .............. ${1:$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE}",
"$LINE_COMMENT$LINE_COMMENT Updated by: ........... ${2:author}",
"$LINE_COMMENT$LINE_COMMENT Changes made: ......... _",
" (${3:change number}) ${4:description}"
]
},
"VBA Linefeed": {
"body": "& vbCrLf ",
"description": "Insert linefeed character",
"prefix": "vbalf"
},
"While": {
"description": "While Wend code block",
"prefix": "vbaWhile",
"body": [
"While ${1:condition}",
"\t$0",
"Wend"
]
},
"With": {
"description": "With code block",
"prefix": "vbaWith",
"body": [
"With $1",
"\t$2",
"End With"
]
},
"Worksheets": {
"description": "Worksheets",
"prefix": "vbaWorksheets",
"body": [
"Worksheets(\"$1\")"
]
},
"vbAbortRetryIgnore": {
"description": "Display Abort, Retry, and Ignore buttons",
"prefix": "vbaBtnAbortRetryIgnore",
"body": [
"vbAbortRetryIgnore"
]
},
"vbApplicationModal": {
"description": "Respond to win before continue",
"prefix": "vbaApplicationModal",
"body": [
"vbApplicationModal"
]
},
"vbCritical": {
"description": "Display Critical Message icon",
"prefix": "vbaBtnCritical",
"body": [
"vbCritical"
]
},
"vbDefaultButton1": {
"description": "First button is default",
"prefix": "vbaBtnDefaultButton1",
"body": [
"vbDefaultButton1"
]
},
"vbDefaultButton2": {
"description": "Second button is default",
"prefix": "vbaBtnDefaultButton2",
"body": [
"vbDefaultButton2"
]
},
"vbDefaultButton3": {
"description": "Third button is default",
"prefix": "vbaBtnDefaultButton3",
"body": [
"vbDefaultButton3"
]
},
"vbDefaultButton4": {
"description": "Fourth button is default",
"prefix": "vbaBtnDefaultButton4",
"body": [
"vbDefaultButton4"
]
},
"vbExclamation": {
"description": "Display Warning Message icon",
"prefix": "vbaBtnExclamation",
"body": [
"vbExclamation"
]
},
"vbInformation": {
"description": "Display Information Message icon",
"prefix": "vbaBtnInformation",
"body": [
"vbInformation"
]
},
"vbOkOnly": {
"description": "Display OK button only",
"prefix": "vbaBtnOkOnly",
"body": [
"vbOkOnly"
]
},
"vbQuestion": {
"description": "Display Warning Query icon",
"prefix": "vbaBtnQuestion",
"body": [
"vbQuestion"
]
},
"vbRetryCancel": {
"description": "Display OK button only",
"prefix": "vbaBtnRetryCancel",
"body": [
"vbRetryCancel"
]
},
"vbSystemModal": {
"description": "Suspend app until user response",
"prefix": "vbaBtnSystemModal",
"body": [
"vbSystemModal"
]
},
"vbYesNo": {
"description": "Display OK button only",
"prefix": "vbaBtnYesNo",
"body": [
"vbYesNo"
]
},
"vbYesNoCancel": {
"description": "Display OK button only",
"prefix": "vbaBtnYesNoCancel",
"body": [
"vbYesNoCancel"
]
},
"vbaOKCancel": {
"description": "Display OK and Cancel buttons",
"prefix": "vbBtnOkCancel",
"body": [
"vbOKCancel"
]
}
}
summary:
description: Provides a brief description of what the function does.
name:
description: The name of the function.
value:
description: The return value of the function.
param:
description: Describes the parameters that the function expects.
name:
description: The name of the parameter.
typeparam:
description: Describes a type parameter for a generic function.
returns:
description: Describes what the function returns.
seealso:
description: Provides a link to related documentation or resources that are related to the function.
cref:
description: The fully qualified name of the member to which this comment refers.
example:
description: Provides an example of how to use the function.
exception:
description: Describes any exceptions or errors that the function may throw.
cref:
description: The fully qualified name of the type that represents the exception.
include:
description: Includes external documentation from another file.
c:
description: Formats text as code.
code:
description: Formats a block of text as code.
remarks:
description: Provides additional information about the function or its behavior.
deprecated:
description: [deprecated] Indicates that the function is deprecated and should not be used.
version:
description: Indicates the version of the code or assembly.
typeparam:
description: Describes a type parameter for a generic function.
name:
description: The name of the type parameter.
typeparamref:
description: References a type parameter for a generic function.
name:
description: The name of the type parameter.
value:
description: Describes the value of a property.
permission:
description: Describes the permissions required to call the function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment