Multiple Arrays and Loops

Peteor

Board Regular
Joined
Mar 16, 2018
Messages
152
I am comparing 2 arrays, and based off the comparison, placing values from an array into a range using the active cell. I am not receiving errors, but two things are happening.

1. Excel (not my machine) locks up when I run the code - How can I improve my code so this does not happen

2. I believe the information is mismatching within my arrays. element 1 of myArray corresponds with element 1 of myArray2, the same for element 2, etc. How do I define bounds telling the computer to start with element 1 of each array?

Here is my code:

Code:
Sub Example()


Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False


Dim myArray() As Variant
Dim myArray2() As Variant
Dim i As Long
Dim rng As Range
Dim element As Variant
Dim element2 As Variant


myArray = Range("B3:B128").Value
myArray2 = Range("C3:C128").Value




If ActiveSheet.Name = "Example Sheet 1" Then
Worksheets("Example Sheet 2").Activate
If ActiveSheet.Name = "Example Sheet 3" Then
Worksheets("Example Sheet 4").Activate
Else
End If
End If


Set rng = ActiveSheet.Range("M2:M5000")


For Each element In myArray
For Each element2 In myArray2
    
    For Each cell In rng
    
        If cell.Value = element Then
        cell.Activate
        cell.Offset(columnoffset:=2).Activate
        ActiveCell.Value = element2
        Else
        End If
        
    Next cell


Next element2
Next element


Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True


End Sub
 
Last edited by a moderator:
That's the one. Sorry for the additional related questions. I have tested, the macro works exactly as intended.
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You're welcome & thanks for the feedback
 
Upvote 0
Fluff,

How do I accomplish this if I wanted to use columns B & D for variable Ary rather than columns B & C?
 
Upvote 0
Change this
Code:
Ary = Range("B3:C128").Value2
to
Code:
Ary = Range("B3:D128").Value2
and this
Code:
.Item(Ary(i, 1)) = Ary(i, 2)
to
Code:
.Item(Ary(i, 1)) = Ary(i, 3)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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