This is currently the code that I am using. I embedded the code inside a change event when a cell within a range changes in runs the loop searching for numbers in the range and posting them in another sheet. But, every time I make a change to a cell within the range the entire loop starts from the top of the column and inserts the data in the destination cells repetitively and multiple times resulting in having the same data multiple times on the sheet. Is there a way to change the code below to prevent the loop from searching the column once the loop already searched a specific cell within the range?
Sub worksheet_Change(ByVal target As Range)
If Not Application.Intersect(target, Range("D2:D30")) Is Nothing Then
Application.EnableEvents = False
Dim wsInfoSheet As Worksheet
Dim wsProofSheet As Worksheet
Dim lngLastRow As Long
Dim r As Long
Dim sAcct As String
Dim lngNextRow As Long
Dim sLongName As String
Dim arrRef() As Variant
Dim arrNames() As String
Dim i As Long
Dim lngRowInNames As Long
Dim lngFoundName As Long
Set wsInfoSheet = ThisWorkbook.Sheets("Info Input")
Set wsProofSheet = ThisWorkbook.Sheets("Proof")
'Will be used in the Proof sheet
lngNextRow = 4 ' waiting to adjust to normal table format
arrRef = wsProofSheet.Range("A199:L79000").Value
ReDim arrNames(1 To UBound(arrRef, 1) + 1, 1 To 2)
With wsInfoSheet
lngLastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
lngRowInNames = 1
For r = 2 To lngLastRow
sAcct = .Cells(r, "E")
'lookup for sAcct in arrRef
For i = 1 To UBound(arrRef, 1)
If arrRef(i, 1) = sAcct Then
sLongName = arrRef(i, 12) '(row i, column 2 from arrRef)
arrNames(lngRowInNames, 1) = sLongName
arrNames(lngRowInNames, 2) = lngNextRow
lngRowInNames = lngRowInNames + 1
Exit For
End If
Next
'lookup for sLongName in arrNames
For i = 1 To UBound(arrNames, 1)
If arrNames(i, 1) = sLongName Then
lngFoundName = i
Exit For
End If
Next
'if the name is new
If arrNames(lngFoundName + 1, 1) = "" Then
wsProofSheet.Cells(lngNextRow, "E") = sAcct
wsProofSheet.Cells(lngNextRow, "B") = sLongName
lngNextRow = lngNextRow + 8 ' would be nicer to just add one row (see first note)
'if the name already exists
Else
wsProofSheet.Cells(arrNames(lngFoundName, 2), wsProofSheet.Cells(arrNames(lngFoundName, 2), wsProofSheet.Columns.Count).End(xlToLeft).Column + 3) = sAcct
End If
Next 'r
End With
Application.EnableEvents = True
End If
End Sub
Sub worksheet_Change(ByVal target As Range)
If Not Application.Intersect(target, Range("D2:D30")) Is Nothing Then
Application.EnableEvents = False
Dim wsInfoSheet As Worksheet
Dim wsProofSheet As Worksheet
Dim lngLastRow As Long
Dim r As Long
Dim sAcct As String
Dim lngNextRow As Long
Dim sLongName As String
Dim arrRef() As Variant
Dim arrNames() As String
Dim i As Long
Dim lngRowInNames As Long
Dim lngFoundName As Long
Set wsInfoSheet = ThisWorkbook.Sheets("Info Input")
Set wsProofSheet = ThisWorkbook.Sheets("Proof")
'Will be used in the Proof sheet
lngNextRow = 4 ' waiting to adjust to normal table format
arrRef = wsProofSheet.Range("A199:L79000").Value
ReDim arrNames(1 To UBound(arrRef, 1) + 1, 1 To 2)
With wsInfoSheet
lngLastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
lngRowInNames = 1
For r = 2 To lngLastRow
sAcct = .Cells(r, "E")
'lookup for sAcct in arrRef
For i = 1 To UBound(arrRef, 1)
If arrRef(i, 1) = sAcct Then
sLongName = arrRef(i, 12) '(row i, column 2 from arrRef)
arrNames(lngRowInNames, 1) = sLongName
arrNames(lngRowInNames, 2) = lngNextRow
lngRowInNames = lngRowInNames + 1
Exit For
End If
Next
'lookup for sLongName in arrNames
For i = 1 To UBound(arrNames, 1)
If arrNames(i, 1) = sLongName Then
lngFoundName = i
Exit For
End If
Next
'if the name is new
If arrNames(lngFoundName + 1, 1) = "" Then
wsProofSheet.Cells(lngNextRow, "E") = sAcct
wsProofSheet.Cells(lngNextRow, "B") = sLongName
lngNextRow = lngNextRow + 8 ' would be nicer to just add one row (see first note)
'if the name already exists
Else
wsProofSheet.Cells(arrNames(lngFoundName, 2), wsProofSheet.Cells(arrNames(lngFoundName, 2), wsProofSheet.Columns.Count).End(xlToLeft).Column + 3) = sAcct
End If
Next 'r
End With
Application.EnableEvents = True
End If
End Sub