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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

farmerscott

Well-known Member
Joined
Jan 26, 2013
Messages
814
Office Version
  1. 365
Platform
  1. Windows
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
 

sericom

Well-known Member
Joined
Jan 19, 2006
Messages
915
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
 

TylrA

New Member
Joined
Aug 3, 2015
Messages
5
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?
 

farmerscott

Well-known Member
Joined
Jan 26, 2013
Messages
814
Office Version
  1. 365
Platform
  1. Windows
Glad that you got it working.

Remember you don't need to use "select" to do something to a cell or range.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,254
Messages
5,769,055
Members
425,513
Latest member
9375021758

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