Looping through an array to perform functions

vbaNumpty

Board Regular
Joined
Apr 20, 2021
Messages
160
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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Snakehips

Well-known Member
Joined
May 17, 2009
Messages
5,365
Office Version
  1. 2013
Platform
  1. Windows
@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
 
Solution

vbaNumpty

Board Regular
Joined
Apr 20, 2021
Messages
160
Office Version
  1. 365
Platform
  1. Windows
@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!
 

Forum statistics

Threads
1,141,816
Messages
5,708,753
Members
421,588
Latest member
Wawie

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
Top