Index Command In VBA

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a range in my worksheet called DMM_Range which is 1 column wide by 50 rows. In VBA, I would like to refer to each row in the range in turn in a loop.

I tried the code below, but the "Index" command is not acceptable to VBA. Can anyone help?

Thanks,

MikeG


For d = 1 To t

Cur_DMM = Index(Range("DMM_Range"), d, 1)

'Do stuff based on Cur_DMM

Next d
 

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.
Hi, Mike,


Index is accessible in VBA as WorksheetFunction.Index, but you don't need it here.
Code:
    For d = 1 To t
        Cur_DMM = Range("DMM_Range")(d, 1)
    Next d
 
Upvote 0
Hi, Mike,


Index is accessible in VBA as WorksheetFunction.Index, but you don't need it here.
Code:
    For d = 1 To t
        Cur_DMM = Range("DMM_Range")(d, 1)
    Next d

Perfect!

Thanks shg

Mike
 
Upvote 0
You're welcome.

Don't forget to declare your variables ...
 
Upvote 0
Sorry - one other thing.

Range("DMM_Range") has text values in (peoples's names with spaces) and is located in another worksheet.

When I read a value form this range into Cur_DMM I get "Empty" (although oddly enough, the first time I tested the macro, it did have the right name in it).

Do I need to have a Dim statement for Cur_DMM at the top of the macro?

Thanks,

MikeG
 
Upvote 0
Add this line to the code:

Code:
Application.GoTo Range("DMM_Range")

Does that select the correct range?
 
Upvote 0
Add this line to the code:

Code:
Application.GoTo Range("DMM_Range")

Does that select the correct range?

It does - and now the text variable reads OK. Thanks, not sure if it was something else I did or if the new line did it.
 
Upvote 0
You can remove the line -- it was just for testing.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,282
Members
452,902
Latest member
Knuddeluff

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