'#########################################################
'Function DataFromHdr() Checks header exists in table
' and if so gets the table data for that header.
'#########################################################
Function DataFromHdr(ByVal TblName As ListObject, ByVal TblHdrName As String) As Variant
Dim HdrOK As Boolean
Dim HdrChk As Range
Dim DataOK As Boolean
Dim HdrRow As Long
Dim HdrCol As Long
Dim StrRow As Long
Dim StrCol As Long
Dim EndRow As Long
Dim EndCol As Long
Dim HdrRange As Range
Dim DataRng As Variant
' Check the header is in the table
HdrOK = False
For Each HdrRange In TblName.HeaderRowRange.Cells
If HdrRange.Value = TblHdrName Then
HdrOK = True
HdrCol = HdrRange.Column
Exit For
End If
Next
If HdrOK = False Then
DataFromHdr = -3
Exit Function
End If
' Check the data range is in the table
DataOK = False
' Extract data funder the Header
With TblName.DataBodyRange '.Range
StrRow = .Row
EndRow = .Rows.Count + StrRow - 1
StrCol = .Column
EndCol = .Columns.Count + StrCol - 1
If HdrCol >= StrCol And HdrCol <= EndCol Then
DataRng = TblName.ListColumns(TblHdrName).DataBodyRange
Else
DataOK = True
DataFromHdr = -2
Exit Function
End If
End With
DataFromHdr = DataRng
End Function
'#########################################################
'Function DelDupArray() Remove blank lines and
' duplicates from an array.
'#########################################################
Function DelDupArray(InputArray As Variant) As Boolean
Dim LBArr As Long, UBArr As Long, NdX As Long
Dim Ast As Long, AEnd As Long, ACnt As Long, BCnt As Long
On Error GoTo ErrorCheck
If IsEmpty(InputArray) Then
ErrMsg = "No array data."
GoTo ErrorCheck
End If
DelDupArray = False
LBArr = LBound(InputArray)
UBArr = UBound(InputArray)
'check if 1st element is blank
If InputArray(LBArr) = "" Then
For NdX = LBArr To UBound(InputArray) - 1
InputArray(NdX) = InputArray(NdX + 1)
Next NdX
ReDim Preserve InputArray(LBound(InputArray) To UBound(InputArray) - 1)
LBArr = LBound(InputArray)
UBArr = UBound(InputArray)
End If
'Remove duplicates and blanks
ErrMsg = "Failed removing duplicates and blanks."
Application.StatusBar = "Removing duplicate lines . . ."
Ast = LBArr
AEnd = UBArr
ACnt = Ast
BCnt = Ast + 1
Do While ACnt <= AEnd
Do While BCnt <= AEnd
If InputArray(ACnt) = InputArray(BCnt) Or InputArray(BCnt) = "" Then
For NdX = BCnt To UBound(InputArray) - 1
InputArray(NdX) = InputArray(NdX + 1)
Next NdX
ReDim Preserve InputArray(LBound(InputArray) To UBound(InputArray) - 1)
AEnd = AEnd - 1
Else
BCnt = BCnt + 1
End If
Loop
ACnt = ACnt + 1
BCnt = ACnt + 1
Loop
DelDupArray = True
On Error GoTo 0
Application.StatusBar = ""
Exit Function
ErrorCheck:
'Reset the application to its normal operating environment.
MsgBox ErrMsg, vbCritical, ThisWorkbook.Name
DelDupArray = False
On Error GoTo 0
Application.StatusBar = ""
End Function
'#########################################################
'Function BubbleSortArray() Sorts array in to ascending
' or descending order. <1 ascending, >=1 descending
'#########################################################
Function BubbleSortArray(InputArray As Variant, ByVal SortOrder As Integer) As Boolean
Dim TempElement As Variant
Dim NdX As Integer
Dim NoExchanges As Integer
On Error GoTo ErrorCheck
If IsEmpty(InputArray) Then
ErrMsg = "No array data."
GoTo ErrorCheck
End If
On Error GoTo ErrorCheck
BubbleSortArray = False
ErrMsg = "Array sort failed."
' Loop until no more "exchanges" are made.
Do
NoExchanges = True
' Loop through each element in the array.
For NdX = 1 To UBound(InputArray) - 1
If SortOrder < 1 Then '<1 ascending, >=1 descending
If InputArray(NdX) > InputArray(NdX + 1) Then
NoExchanges = False
TempElement = InputArray(NdX)
InputArray(NdX) = InputArray(NdX + 1)
InputArray(NdX + 1) = TempElement
End If
Else
If InputArray(NdX) < InputArray(NdX + 1) Then
NoExchanges = False
TempElement = InputArray(NdX)
InputArray(NdX) = InputArray(NdX + 1)
InputArray(NdX + 1) = TempElement
End If
End If
Next NdX
Loop While Not (NoExchanges)
BubbleSortArray = True
Exit Function
ErrorCheck:
'Reset the application to its normal operating environment.
MsgBox ErrMsg, vbCritical, ThisWorkbook.Name
On Error GoTo 0
BubbleSortArray = False
End Function