How to offset a for loop?

Waimea

Active Member
Joined
Jun 30, 2018
Messages
465
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
    Data = Sheets("Data").Range("Values2020").Value
   
    For i = 1 To UBound(Data)
   
         area = Data(i, 1)
         aval = Data(i, 2)
               
        myFunc area, aval
                
     Next i

First row is headers.

Column A contains names, column B to F contains values, named Values2020 to Values2025.

I am trying to offset Data somehow so that I can loop from column B to column F or column X...

I have tried with an outer loop:

VBA Code:
Dim j As Integer

For j = 1 to 5
    Data = Sheets("Data").Range("Values202" & j).Value
Next j

Any suggestions?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Is that not working?

If it isn't how?
 
Upvote 0
Is that not working?

If it isn't how?
Hi Norie,

thank you for your reply.

The outer loop works if i copy column A and B and make them a range.

I then copy column A and C, column A and D etc.

But I want to have one column A with names and the other columns with data.

Is this possible?
 
Upvote 0
Try to multiply column, do something like this...
VBA Code:
Dim area, aval, data, i
Dim varColumn

Sub OffSet()

    data = Sheets("Data").Range("Values2020").Value
    varColumn = 3
    For i = 1 To UBound(data)
         area = data(i, 1 * varColumn)
         aval = data(i, 1 * varColumn)    
        MsgBox (area & aval)    
     Next i

End Sub
 
Upvote 0
Sorry, I mean...
VBA Code:
       area = data(i, 1 * varColumn)
       aval = data(i, 2 * varColumn)
 
Upvote 0
Hi EXCEL MAX,

thank you for your reply!

However, I get a subscript out of range with your updated code!
 
Upvote 0
I want to turn:

VBA Code:
area = Data(i, 1)
aval = Data(i, 2)

Into:

VBA Code:
area = Data(i, 1)
aval = Data(i, 3)

And then:

VBA Code:
area = Data(i, 1)
aval = Data(i, 4)

etc.
 
Upvote 0
Try to set range ...........Sheets("Data").Range("A1:F5").Value
or set name for the range to "Values2020".
 
Upvote 0
Hi EXCEL MAX,

thank you for your reply!

How do you mean?
 
Upvote 0
Why not put all the data in one array?
VBA Code:
Data = Sheets("Data").Range("A1").CurrentRegion.Value

For idxCol= 2 To 6
    For idxRow = LBound(Data,1) To UBound(Data,1)
        area = Data(idxRow, 1)
        aval = Data(idxRow, idxCol)
        myFunc area, aval
    Next idxRow
Next idxCol
 
Upvote 0
Solution

Forum statistics

Threads
1,214,821
Messages
6,121,755
Members
449,049
Latest member
excelknuckles

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