Skip to content

Instantly share code, notes, and snippets.

@neelsg
neelsg / ExcelStyleFormulas.vbs
Created November 25, 2014 10:43
Functions that allow easier translation from Excel formulas into VBScript formulas
Function FxMin(ByVal ThisArray)
'Return the minimum value in an array (Excel style)
Dim Res, LBnd, UBnd, Idx
LBnd = LBound(ThisArray)
UBnd = UBound(ThisArray)
Res = ThisArray(LBnd)
If UBnd > LBnd Then
@neelsg
neelsg / FindCount.vba
Created November 3, 2014 11:56
Count the number of times a string is found in another string. VBA / VBScript
Public Function FindCount(Needle As String, Haystack As String) As Long
'See how many times the Needle occurs in the Haystack
Dim Res As Long
Dim Temp As String
Res = 0
If Len(Needle) <> 0 Then
Temp = Replace(Haystack, Needle, "")
@neelsg
neelsg / DecodeXML.vba
Created November 3, 2014 11:53
Functions to decode or encode text for use in an XML file. VBScript / VBA
Public Function DecodeXml(TextToDecode As String) As String
'Take text that has been encoded for XML and change it to normal text
Dim Res As String
Res = Replace(Replace(Replace(Replace(TextToDecode, "&quot;", """"), "&gt;", ">"), "&lt;", "<"), "&amp;", "&")
DecodeXml = Res
End Function
@neelsg
neelsg / GetColLetter.vba
Created November 3, 2014 11:41
Functions to convert column number to letter and letter to number. Excel VBA
Public Function GetColLetter(ByVal ColNum As Long) As String
'Get the letter of a column based on the number
Dim Res As String
Res = Split(Cells(1, ColNum).Address, "$")(1)
GetColLetter = Res
End Function
@neelsg
neelsg / WriteToFile.vbs
Created November 3, 2014 11:35
Append text to file. VBScript / VBA
Sub WriteToFile(ByVal FilePath, ByVal TxtToWrite)
'Write any line of text to the end of a file. If the file does not exist, try to create it
On Error Resume Next
Dim FileSysObj, FileHandle
Set FileSysObj = CreateObject("Scripting.FileSystemObject")
If FileSysObj.FileExists(FilePath) Then
@neelsg
neelsg / QuickSort.vbs
Created November 3, 2014 09:29
Quicksort implementation in VBScript / VBA
Sub QuickSort(ByRef ThisArray)
'Sort an array alphabetically
Dim LowerBound, UpperBound
LowerBound = LBound(ThisArray)
UpperBound = UBound(ThisArray)
QuickSortRecursive ThisArray, LowerBound, UpperBound
End Sub
@neelsg
neelsg / InArray.vbs
Created November 3, 2014 09:27
Function to check if an item is found in an array. VBScript / VBA
Function InArray(ByVal ElementToFind, ByVal ArrayToSearch)
'Look to see if an element is in an array.
'Return index of element if found
'Return -1 if not found
Dim Res, LBnd, UBnd, Idx
Res = -1
If IsArray(ArrayToSearch) Then