Good morning, all.
I'm somewhat new to VBA, so I need your expert help with something. I have a UDF that looks for a Unique ID (quote number) in a range/array and returns all the corresponding 3-letter codes into a single cell. I only want each 3-letter code to be returned once even though there will be many occurrences of each.
I think the slowest part of the code is in the beginning section that I've marked with a comment.
Thank you in advance for your help!
Here's the code:
Option Base 1
Option Explicit
Public Function MHC(QuoteNumber As Range)
Dim QuoteNumStart As Integer
Dim QuoteNumCount As Integer
Dim MrkPrFxRng As Range
Dim QuoteNumTrim()
Dim x
Dim xCounter
xCounter = 0
'Fill QuoteNumTrim with values in the "QuoteNum_ColRef" range
'this is the section of code that could use the most improvement
For x = 1 To ThisWorkbook.Worksheets("MRDW group pickup").Range("QuoteNum_ColRef").Count Step 1
xCounter = xCounter + 1
ReDim Preserve QuoteNumTrim(xCounter)
QuoteNumTrim(x) = Trim(ThisWorkbook.Worksheets("MRDW group pickup").Range("QuoteNum_ColRef")(x, 1))
Next
'Find the first occurrence of the quote number
QuoteNumStart = Application.WorksheetFunction.Match(QuoteNumber, QuoteNumTrim, 0) + 1
'Count the number of times that quote number is found and assign it to QuoteNumCount
Set x = Nothing
Set xCounter = Nothing
xCounter = 0
For x = 1 To UBound(QuoteNumTrim) Step 1
If QuoteNumTrim(x) = QuoteNumber Then
xCounter = xCounter + 1
End If
Next
QuoteNumCount = xCounter
'Find Market Prefix name column
Dim HdrVar
Dim HdrUsedCols
Dim MrkPrFxCol
HdrUsedCols = ThisWorkbook.Sheets("MRDW group pickup").UsedRange.Columns.Count
For HdrVar = 1 To HdrUsedCols Step 1
If ThisWorkbook.Sheets("MRDW group pickup").Cells(1, HdrVar) = "Market Prfx Name for Report" Then
MrkPrFxCol = HdrVar
Exit For
End If
Next
'identify the range of mini hotel codes
Set MrkPrFxRng = ThisWorkbook.Sheets("MRDW group pickup").Range(ThisWorkbook.Sheets("MRDW group pickup").Cells(QuoteNumStart, MrkPrFxCol).Address, ThisWorkbook.Sheets("MRDW group pickup").Cells((QuoteNumStart + QuoteNumCount - 1), MrkPrFxCol).Address)
'fill an array with 1 of each corresponding mini hotel code
Dim MrkPrFxArray()
Dim i
Dim UniqueCounter
UniqueCounter = 0
For i = 2 To MrkPrFxRng.Count + 1 Step 1
If (i = MrkPrFxRng.Count) And (MrkPrFxRng(i - 1, 1) <> MrkPrFxRng(i, 1)) Then
UniqueCounter = UniqueCounter + 1
ReDim Preserve MrkPrFxArray(1 To UniqueCounter)
MrkPrFxArray(UniqueCounter) = Left(MrkPrFxRng(i, 1), 3)
End If
If MrkPrFxRng(i, 1) <> MrkPrFxRng(i - 1, 1) Then
UniqueCounter = UniqueCounter + 1
ReDim Preserve MrkPrFxArray(1 To UniqueCounter)
MrkPrFxArray(UniqueCounter) = Left(MrkPrFxRng(i - 1, 1), 3)
End If
Next
'create string of Mini Hotel Codes
Set i = Nothing
Dim MHCString
MHCString = ""
For i = 1 To UBound(MrkPrFxArray)
MHCString = MHCString + MrkPrFxArray(i) + ", "
If i = UBound(MrkPrFxArray) Then
MHCString = Left(MHCString, Len(MHCString) - 2)
End If
Next
MHC = MHCString
End Function
I'm somewhat new to VBA, so I need your expert help with something. I have a UDF that looks for a Unique ID (quote number) in a range/array and returns all the corresponding 3-letter codes into a single cell. I only want each 3-letter code to be returned once even though there will be many occurrences of each.
I think the slowest part of the code is in the beginning section that I've marked with a comment.
Thank you in advance for your help!
Here's the code:
Option Base 1
Option Explicit
Public Function MHC(QuoteNumber As Range)
Dim QuoteNumStart As Integer
Dim QuoteNumCount As Integer
Dim MrkPrFxRng As Range
Dim QuoteNumTrim()
Dim x
Dim xCounter
xCounter = 0
'Fill QuoteNumTrim with values in the "QuoteNum_ColRef" range
'this is the section of code that could use the most improvement
For x = 1 To ThisWorkbook.Worksheets("MRDW group pickup").Range("QuoteNum_ColRef").Count Step 1
xCounter = xCounter + 1
ReDim Preserve QuoteNumTrim(xCounter)
QuoteNumTrim(x) = Trim(ThisWorkbook.Worksheets("MRDW group pickup").Range("QuoteNum_ColRef")(x, 1))
Next
'Find the first occurrence of the quote number
QuoteNumStart = Application.WorksheetFunction.Match(QuoteNumber, QuoteNumTrim, 0) + 1
'Count the number of times that quote number is found and assign it to QuoteNumCount
Set x = Nothing
Set xCounter = Nothing
xCounter = 0
For x = 1 To UBound(QuoteNumTrim) Step 1
If QuoteNumTrim(x) = QuoteNumber Then
xCounter = xCounter + 1
End If
Next
QuoteNumCount = xCounter
'Find Market Prefix name column
Dim HdrVar
Dim HdrUsedCols
Dim MrkPrFxCol
HdrUsedCols = ThisWorkbook.Sheets("MRDW group pickup").UsedRange.Columns.Count
For HdrVar = 1 To HdrUsedCols Step 1
If ThisWorkbook.Sheets("MRDW group pickup").Cells(1, HdrVar) = "Market Prfx Name for Report" Then
MrkPrFxCol = HdrVar
Exit For
End If
Next
'identify the range of mini hotel codes
Set MrkPrFxRng = ThisWorkbook.Sheets("MRDW group pickup").Range(ThisWorkbook.Sheets("MRDW group pickup").Cells(QuoteNumStart, MrkPrFxCol).Address, ThisWorkbook.Sheets("MRDW group pickup").Cells((QuoteNumStart + QuoteNumCount - 1), MrkPrFxCol).Address)
'fill an array with 1 of each corresponding mini hotel code
Dim MrkPrFxArray()
Dim i
Dim UniqueCounter
UniqueCounter = 0
For i = 2 To MrkPrFxRng.Count + 1 Step 1
If (i = MrkPrFxRng.Count) And (MrkPrFxRng(i - 1, 1) <> MrkPrFxRng(i, 1)) Then
UniqueCounter = UniqueCounter + 1
ReDim Preserve MrkPrFxArray(1 To UniqueCounter)
MrkPrFxArray(UniqueCounter) = Left(MrkPrFxRng(i, 1), 3)
End If
If MrkPrFxRng(i, 1) <> MrkPrFxRng(i - 1, 1) Then
UniqueCounter = UniqueCounter + 1
ReDim Preserve MrkPrFxArray(1 To UniqueCounter)
MrkPrFxArray(UniqueCounter) = Left(MrkPrFxRng(i - 1, 1), 3)
End If
Next
'create string of Mini Hotel Codes
Set i = Nothing
Dim MHCString
MHCString = ""
For i = 1 To UBound(MrkPrFxArray)
MHCString = MHCString + MrkPrFxArray(i) + ", "
If i = UBound(MrkPrFxArray) Then
MHCString = Left(MHCString, Len(MHCString) - 2)
End If
Next
MHC = MHCString
End Function