Array Copy to Range Loop

JJorsi

New Member
Joined
Dec 20, 2016
Messages
6
Having trouble with the code below. Purpose is to sort data from one sheet into an array to match certain data to a range in another sheet in order to copy over missing data. Everything works fine and the first array copies into the first range of data on the new sheet but it then loops back to the beginning of the sub.. Basically the code never ends as a result.. Help or suggestions??

Code:
Sub attendName(fromsh As Variant, tosh As Variant)
'copy attendee names over to match with proper titles

Dim fmArr1(33 To 49, 2) As String
Dim fmArr2(33 To 49, 2) As String
Dim toArr1(33 To 49, 2) As String
Dim toArr2(33 To 49, 2) As String

'filling array with data from from and to sheet
For i = 33 To 49
fmArr1(i, 0) = Sheets(fromsh).Range("$C$" & i).Value
fmArr1(i, 1) = Sheets(fromsh).Range("$D$" & i).Value
fmArr1(i, 2) = Sheets(fromsh).Range("$E$" & i).Value
fmArr2(i, 0) = Sheets(fromsh).Range("$I$" & i).Value
fmArr2(i, 1) = Sheets(fromsh).Range("$J$" & i).Value
fmArr2(i, 2) = Sheets(fromsh).Range("$K$" & i).Value

toArr1(i, 0) = Sheets(tosh).Range("$C$" & i).Value
toArr1(i, 1) = Sheets(tosh).Range("$D$" & i).Value
toArr1(i, 2) = Sheets(tosh).Range("$E$" & i).Value
toArr2(i, 0) = Sheets(tosh).Range("$I$" & i).Value
toArr2(i, 1) = Sheets(tosh).Range("$J$" & i).Value
toArr2(i, 2) = Sheets(tosh).Range("$K$" & i).Value

Next i

'matches attendee titles between sheets and copies the name into array
For i = LBound(fmArr1) To UBound(fmArr1)
For j = LBound(fmArr1) To UBound(fmArr1)​
If fmArr1(i, 0) = toArr1(j, 0) And fmArr1(i, 0) <> blank Then​
toArr1(j, 2) = fmArr1(i, 2)
ElseIf fmArr2(i, 0) = toArr1(j, 0) And fmArr2(i, 0) <> blank Then​
toArr1(j, 2) = fmArr2(i, 2)
ElseIf fmArr1(i, 0) = toArr2(j, 0) And fmArr1(i, 0) <> blank Then​
toArr2(j, 2) = fmArr1(i, 2)
ElseIf fmArr2(i, 0) = toArr2(j, 0) And fmArr2(i, 0) <> blank Then​
toArr2(j, 2) = fmArr2(i, 2)
End If​
Next j​
Next i
'paste array names into to sheet
Sheets(tosh).Range("$C$33:$E$49").Value = toArr1 'code hits this point, copies fine and then loops back to beginning of sub...
Sheets(tosh).Range("$I$33:$K$49").Value = toArr2 'code never reaches this point

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I don't see anything that would loop back in your code...

By chance is this procedure invoked via Worksheet_Change event?
 
Upvote 0
No, this code is in the Module and is called when a button triggers migration from one sheet to another. It isn't linked to the Worksheet_Change event as far as I can tell..

I don't know what is triggering the loop either, it fills in either range fine but loops before it fills in the second range...
 
Upvote 0
Update: this code is triggered by the Worksheet_Change event.. this calls a different sub that calls a second sub that then calls my code..
 
Upvote 0
Hmm, I'd imagine that infinite loop can be avoided by setting Application.EnableEvents to false at start of Worksheet_Change event and turning it back to True at the end.

My guess is that infinite loop is caused by Worksheet_Change being triggered when data array is written to range.value
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top