Looping through an array to perform functions

vbaNumpty

Board Regular
Joined
Apr 20, 2021
Messages
171
Office Version
  1. 365
Platform
  1. Windows
I am having trouble figuring out how to write code that will go through my array and perform actions based on a cells value.

I have an array set to a dynamic range and I am looking to have it parse the rows and look at the date value in the second column of the range and perform actions when the date is < the current date.

My code is

VBA Code:
 Dim ordrList As Range
    Dim i As Integer
    Dim j As Integer
    Dim x As Integer
    Dim row As Range
    
    i = Sheet3.Range("T1").value  'number of lines (orders) from import data
    Set ordrList = Sheet3.Range("outdata")
    
    myarray = Sheet3.Range("outdata")
    
    For j = 1 To i
        For x = 1 To UBound(myarray, 2)
        Debug.Print (myarray(i, j))
        Next x
        
    Next j

Trying to play around with variables to get it to output the values, but the above is just printing the last value in the first column 12 times. I don't fully understand the Ubound argument either. If anyone could give me the framework for having the loop look at the value in the second column of the 12 column wide array and taking action that would be greatly appreciated!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
@vbaNumpty does this edit help any...

VBA Code:
Dim ordrList As Range
    Dim i As Integer
    Dim j As Integer
    Dim x As Integer
    Dim row As Range
    Dim myarray As Variant
    
    i = Sheet3.Range("T1").Value  'number of lines (orders) from import data
    Set ordrList = Sheet3.Range("outdata")
    
    myarray = ordrList
    
    For j = 1 To i
        If myarray(j, 2) < Date Then
        'do some stuff eg here is the value from columns 2 and 12 of the named range
        MsgBox "Date is  " & myarray(j, 2) & myarray(j, 12) & "is in col 12"
        End If
        
    Next j
 
Upvote 0
Solution
@vbaNumpty does this edit help any...

VBA Code:
Dim ordrList As Range
    Dim i As Integer
    Dim j As Integer
    Dim x As Integer
    Dim row As Range
    Dim myarray As Variant
   
    i = Sheet3.Range("T1").Value  'number of lines (orders) from import data
    Set ordrList = Sheet3.Range("outdata")
   
    myarray = ordrList
   
    For j = 1 To i
        If myarray(j, 2) < Date Then
        'do some stuff eg here is the value from columns 2 and 12 of the named range
        MsgBox "Date is  " & myarray(j, 2) & myarray(j, 12) & "is in col 12"
        End If
       
    Next j
Great, thank you very much!
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,981
Members
449,058
Latest member
oculus

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