For some reason my loop is creating duplicate outputs. If anyone can help understand why, I would be grateful
Code:
Sub IPutil()
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Dim frow, lrow, fcol, lcol As Integer
Dim rng, MyCell As Range
Dim sh1, sh2, sh3 As Worksheet
Set sh1 = ThisWorkbook.Worksheets("Internal_Providers")
Set sh2 = ThisWorkbook.Worksheets("INTERNAL_PROV_EXPORT")
Set sh3 = ThisWorkbook.Worksheets("SER_TEMPLATE")
''''Gets StartCell Column and Row
fcol = sh1.Cells(1, Columns.Count).End(xlToLeft).Column
frow = sh1.Cells(Rows.Count, 25).End(xlUp).Row
''''''Gets last row on Ser template Sheet
'sh3.Activate
''''Gets StartCell
Set StartCell = sh1.Cells(frow + 1, fcol)
'Find Last Row and Column
LastRow = sh1.Cells(sh1.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sh1.Range("A" & frow).End(xlToRight).Column
'Select Range
Set rng = sh1.Range(StartCell, sh1.Cells(LastRow, LastColumn))
For Each MyCell In rng
If MyCell <> "" Then
ddd = ThisWorkbook.Worksheets("SER_TEMPLATE").Cells(Rows.Count, 3).End(xlUp).Row
v1 = MyCell.Row
'''''Finds Template based on row
Dim Fvalue As String
Dim rngToSearch, rngCurrent As Range
Fvalue = sh1.Cells(v1, 2).Value
Set wks = Sheets("SER_TEMPLATE")
Set rngToSearch = wks.Range("C6", "C" & ddd)
Set rngCurrent = rngToSearch.Find(Fvalue)
''''''If it does not find a match add base template
If rngCurrent Is Nothing Then
dcb = sh2.Cells(Rows.Count, 2).End(xlUp).Row
sh2.Rows(dcb + 1).EntireRow.Value = sh3.Rows(ddd).EntireRow.Value
''''' Adds * for ID
sh2.Cells(dcb + 1, 1).Value = "*"
'''''' Adds Role after template has ran
sh2.Cells(dcb + 1, 3).Value = sh1.Cells(v1, 2).Value
'''''Generate a unique Id
Call IDGenerator
'''''If it finds a match
Else
dcb = sh2.Cells(Rows.Count, 2).End(xlUp).Row
v2 = rngCurrent.Row
sh2.Rows(dcb + 1).EntireRow.Value = sh3.Rows(v2).EntireRow.Value
'''''Adds * for ID
sh2.Cells(dcb + 1, 1).Value = "*"
Call IDGenerator
End If
sh2.Cells(dcb + 1, 2).Value = sh1.Cells(v1, 1).Value
Else
End If
''Call dlte
Next MyCell
'''''Remove Duplicates
End Sub