Skip to content

Instantly share code, notes, and snippets.

@palikhov
Created February 8, 2022 09:20
Show Gist options
  • Save palikhov/5ba8a2a898e055405e1f6052fbee3053 to your computer and use it in GitHub Desktop.
Save palikhov/5ba8a2a898e055405e1f6052fbee3053 to your computer and use it in GitHub Desktop.
Option Explicit
Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)
Dim pivot As Variant
Dim tmpSwap As Variant
Dim tmpLow As Long
Dim tmpHi As Long
tmpLow = inLow
tmpHi = inHi
pivot = vArray((inLow + inHi) \ 2)
While (tmpLow <= tmpHi)
While (vArray(tmpLow) < pivot And tmpLow < inHi)
tmpLow = tmpLow + 1
Wend
While (pivot < vArray(tmpHi) And tmpHi > inLow)
tmpHi = tmpHi - 1
Wend
If (tmpLow <= tmpHi) Then
tmpSwap = vArray(tmpLow)
vArray(tmpLow) = vArray(tmpHi)
vArray(tmpHi) = tmpSwap
tmpLow = tmpLow + 1
tmpHi = tmpHi - 1
End If
Wend
If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi
If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi
End Sub
Public Function WorksheetExists(ByVal WorksheetName As String) As Boolean
Dim Sht As Worksheet
For Each Sht In ThisWorkbook.Worksheets
If Application.Proper(Sht.Name) = Application.Proper(WorksheetName) Then
WorksheetExists = True
Exit Function
End If
Next Sht
WorksheetExists = False
End Function
Public Function FindAAN(NextWord) As String
Dim Vowels(1 To 5) As String
Dim i As Integer
Vowels(1) = "a"
Vowels(2) = "e"
Vowels(3) = "i"
Vowels(4) = "o"
Vowels(5) = "u"
For i = 1 To 5
If LCase(Left(NextWord, 1)) = Vowels(i) Then
FindAAN = "an"
Exit Function
End If
Next
FindAAN = "a"
End Function
Public Function RollDice(XdY) As Double
Dim i As Integer
Dim S As Double
Dim SplitIt() As String
SplitIt() = Split(CStr(XdY), "d")
S = 0
For i = 1 To CInt(SplitIt(0))
S = S + Rand(CInt(SplitIt(1)), 1, False)
Next
RollDice = S
End Function
Public Function Contains(Phrase As String, Substring As String, Optional LwrCase As Boolean) As Boolean
Dim P, S As String
If LwrCase = True Then
P = LCase(Phrase)
S = LCase(Substring)
Else
P = Phrase
S = Substring
End If
If Replace(P, S, "") = P Then
Contains = False
Else
Contains = True
End If
End Function
Public Function RITC(Word As String)
Dim i As Integer
Dim Arr(1 To 3) As String
Dim Dum As String
Arr(1) = "("
Arr(2) = ")"
Arr(3) = "-"
Dum = Replace(Word, " ", "")
For i = 1 To UBound(Arr(), 1)
Dum = Replace(Dum, Arr(i), "_")
Next
RITC = Dum
End Function
Public Function Capitalize(Word As String)
Capitalize = UCase(Left(Word, 1)) & LCase(Left(Word, Len(Word) - 1))
End Function
Public Function RandVal(RandArray(), Optional Col = 1, Optional FindUb As Boolean = False, Optional Val, Optional RemoveBlanks As Boolean = False)
Dim Ub, Lb As Double
Dim i, j As Integer
Dim RA() As String
'Dim Dum As String
Ub = UBound(RandArray(), 1)
Lb = LBound(RandArray(), 1)
If FindUb = True Then
For i = Lb To Ub
If RandArray(i, Col) = "" Then
Ub = i - 1
Exit For
End If
Next
End If
If RemoveBlanks = True Then
'get non blank count
j = 0
For i = 1 To Ub
If Not RandArray(i, Col) = "" Then
j = j + 1
End If
Next
'reset
ReDim RA(1 To j) As String
'populate array
j = 0
For i = 1 To Ub
If Not RandArray(i, Col) = "" Then
j = j + 1
RA(j) = RandArray(i, Col)
End If
Next
'get value
Val = Rand(j, Lb, False)
RandVal = RA(Val)
Else
Val = Rand(Ub, Lb, False)
RandVal = RandArray(Val, Col)
End If
End Function
Public Function Rand(Upper, Lower, Deci As Boolean)
Dim Numb As Double
Randomize
Numb = Int((Upper - Lower + 1) * Rnd + Lower)
If Deci = True And Not Numb = Upper Then
Randomize
Numb = Numb + Rnd()
End If
Rand = Numb
End Function
Public Function L2N(ColumnLetter As String)
Dim ColumnNumber As Long
ColumnNumber = Range(ColumnLetter & 1).Column
L2N = ColumnNumber
End Function
Public Function N2L(ColumnNumber As Long)
Dim ColumnLetter As String
ColumnLetter = Split(Cells(1, ColumnNumber).Address, "$")(1)
N2L = ColumnLetter
End Function
Public Function ColumnLetter(RNG As Range)
Dim A, L As String
A = CStr(RNG.Address)
L = Mid(A, 2, InStr(2, A, "$") - 2)
ColumnLetter = L
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment