Good day all,
I have been trying to use the range.find method to find a string from an existing cell in a column in a different sheet in the same workbook. When I use the debugger to walk through the code it never gives a result from the range.find method. I am completely perplexed by this (not an uncommon state for me...) Please review the below code and let me know what I am doing wrong. Thanks!!!
Function Dupe_BU_List(strSuplName As String) As String
Dim sht As Worksheet
Dim ws As Worksheet
Dim rngSearchRange As Range
Dim rngFoundCell As Range
Dim intBUCol As Integer
Dim intLegacyCol As Integer
Dim lngSuplCnt As Long
Dim shtRow As Long
Dim shtColG As String
Dim shtColH As String
Dim strTestSupl As String
Dim strCurrSupl As String
Dim strNextSupl As String
Dim strCurrBU As String
Dim strNextBU As String
Dim strFirstAddress As String
Dim varLegacyIDs As Variant
Dim varBUSuplNames As Variant
Dim strDupe_BU_List As String
Set ws = ThisWorkbook.Sheets("All ADG Suppliers") ' Sheet exists with data
Set sht = ThisWorkbook.Sheets("Name Corr") ' Sheet exists with data
Set rngSearchRange = ws.Range("C:C") 'List being searched is here and is populated and has matching data
intBUCol = 1
intLegacyCol = 2
shtColG = "G"
shtColH = "H"
shtRow = ActiveCell.Row
Dupe_BU_List = ""
strDupe_BU_List = ""
varLegacyIDs = ""
varBUSuplNames = ""
strSuplName = Shortened_Supl_Name(strSuplName)
'**This is where it fails**
**Set rngFoundCell = rngSearchRange.Find(What:=strSuplName, LookIn:=xlValues, LookAt:=xlPart)
If Not rngFoundCell Is Nothing Then
strFirstAddress = rngFoundCell.Address ' Store the first address to avoid infinite loop
strDupe_BU_List = rngFoundCell.Offset(0, intBUCol - rngFoundCell.Column).Value
varLegacyIDs = rngFoundCell.Offset(0, intLegacyCol - rngFoundCell.Column).Value
varBUSuplNames = rngFoundCell.Value
Do While Not rngFoundCell Is Nothing
' Find the next match
Set rngFoundCell = rngSearchRange.FindNext(rngFoundCell)
If Not rngFoundCell Is Nothing Then
If rngFoundCell.Address <> strFirstAddress Then
strDupe_BU_List = strDupe_BU_List & ";" & rngFoundCell.Offset(0, intBUCol - rngFoundCell.Column).Value
varLegacyIDs = varLegacyIDs & ";" & rngFoundCell.Offset(0, intLegacyCol - rngFoundCell.Column).Value
varBUSuplNames = varBUSuplNames & ";" & rngFoundCell.Value
Else
Exit Do
End If
Else
MsgBox ("Issues with range.findnext")
Exit Do
End If
Loop
Else
MsgBox ("Issues with initial range.find")
End If**
If Left(strDupe_BU_List, 1) = ";" Then
Dupe_BU_List = Right(strDupe_BU_List, Len(strDupe_BU_List) - 1)
'sht.Range(shtColG & shtRow).Value = varLegacyIDs
'sht.Range(shtColH & shtRow).Value = varBUSuplNames
Else
Dupe_BU_List = strDupe_BU_List
End If
End Function
Function Shortened_Supl_Name(strLongSuplName) As String
Dim strShortSuplName As String
Dim varPunc As Variant
Dim varElements As Variant
Dim p As Long
Dim e As Long
strShortSuplName = strLongSuplName
varPunc = Array("-", ".", ",")
varElements = Array("-", " ", ".", " ", ",", " DC", " INC", " LLC", " LLP", " L P", " LTD", " NA", " P C", " PC")
For p = LBound(varPunc) To UBound(varPunc)
strShortSuplName = Replace(strShortSuplName, varPunc(p), " ")
Next p
For e = LBound(varElements) To UBound(varElements)
strShortSuplName = Replace(strShortSuplName, varElements(e), " ")
Next e
Shortened_Supl_Name = Trim(strShortSuplName)
End Function
Else : MsgBox "not found '" & strSuplName & "'"afterLoop. Also addElse: Exit DoaftervarBUSuplNames = ...otherwise if a match is found it will enter a infinite loop.Exit Do? You need to exitIf rngFoundCell.Address = strFirstAddress