Skip to content

Instantly share code, notes, and snippets.

@jaykilleen
Last active August 29, 2015 14:17
Show Gist options
  • Save jaykilleen/892d0f0eed87b9f8e6b0 to your computer and use it in GitHub Desktop.
Save jaykilleen/892d0f0eed87b9f8e6b0 to your computer and use it in GitHub Desktop.
Excel VBA 'functions' module
'copy the content of this gist into a new module in VBA (get to VBA by pressing Alt+F11)
'you then have access to the functions below from anywhere else in your workbook VBA scripts
'these scrips can be found at Gist on Github at https://gist.github.com/jaykilleen/892d0f0eed87b9f8e6b0
Option Explicit
Function get_last_row(sheetname As String, column_number As Integer) As Long
get_last_row = Sheets(sheetname).Cells(Rows.Count, column_number).End(xlUp).row
End Function
Function get_column_number(sheet As Worksheet, value As String, Optional row As Variant, Optional whole As Boolean) As Integer
Dim looks As String
If IsMissing(row) Then
row = 1
End If
If whole = False Then
get_column_number = Sheets(sheet.Name).Cells(row, 1).EntireRow.Find(What:=value, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).column
Else
get_column_number = Sheets(sheet.Name).Cells(row, 1).EntireRow.Find(What:=value, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).column
End If
End Function
Function get_row_number(sheet As Worksheet, value As Variant, column As Integer) As Integer
get_row_number = Sheets(sheet.Name).Cells(1, column).EntireColumn.Find(What:=value, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).row
End Function
Function find_in_array(FindMe As String, myArray() As String)
Dim C As String
Dim ItemFound As Boolean
Dim MatchCase As Boolean
MatchCase = False
' Perform search
C = Chr$(1)
ItemFound = InStr(1, C & Join(myArray, C) & C, C & FindMe & C, 1 + MatchCase)
If ItemFound Then find_in_array = True
End Function
Function timestamp(time as string)
timestamp = Format(Now(), "YYYYMMDDhhmmss")
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment