For...Next Statement won't accept datatype argument

TylrA

New Member
Joined
Aug 3, 2015
Messages
5
I'm new to programming and want to make a For loop that selects cells based on the counter:

For indexA = 1 to 10
Range(cells(indexA, indexA + 3)).Select
...
Next indexA

The debugger flags the second line saying "Method 'Range' of object '_Global' failed."
I thought maybe indexA needed to be further defined so I tried

For indexA As Integer = 1 to 10
Range(cells(indexA, indexA +3)).Select
...
Next indexA

Now it won't accept the first line "As Integer" and says "Expected: =" even though there is one.

How can I implement the counter "indexA" in my code, and why isn't it accepting the datatype argument?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
TylrA,

Welcome to the forum.

It is a little unclear from your code if you want to select a single cell or a range.

If you want a single cell use

Code:
Cells(row,column).select

or if you want a range using the cells method-

Code:
Range(cells(row,column),cells(row,column)).select

Hope that helps,

FarmerScott
 
Upvote 0
What cell/s should be selected on the first loop?
You need to specify the variable type on it's own line

Dim indexA As Integer
For indexA = 1 to 10
 
Upvote 0
Thanks! That answers part of my question.

To farmerscott,
Just a cell. I guess it would be better put like this then:

For indexA = 1 to 10
Cells(indexA, indexA + 3).Select
...
Next indexA

I still seem to have an error in the syntax because the debugger stops on line 2. How should I write that?
 
Upvote 0
Glad that you got it working.

Remember you don't need to use "select" to do something to a cell or range.
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

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