Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select cell in loop

Status
Not open for further replies.

drewdaman

Programmer
Aug 5, 2003
302
CA
hello,

i am having trouble selecting cells in a loop. i tried the following code:

Code:
Do While (i < 10)
        'Range(ActiveCell.Offset(i, 0)).Select
        Cells(i, 1).Select
        i = i + 1
    Loop

(also tried teh commented line)

this is just some garbage code to see if this was the problem in my real code.. it seems it was.. i get the error:

"Run time error 1004. Application defined or object defined error"

anyone know what is wrong? and if so.. how can i fix this?

thanks!
 
ah.. let me post the whole sub.. its only a few lines.. but i dont' want you to think i'm not initializing i!

Code:
Sub stupid()
    Dim i As Integer
    i = 0
    'Range("A2").Select
    Cells(2, 1).Select
    Do While (i < 10)
        'Range(ActiveCell.Offset(i, 0)).Select
        Cells(i, 1).Select
        i = i + 1
    Loop
End Sub
 
On the 1st iteration you have:
Cells([highlight]0[/highlight], 1).Select

You wanted this ?
Sub stupid()
Dim i As Integer
i = 2
Do While (i < 10)
Cells(i, 1).Select
i = i + 1
Loop
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
You could start with i=1.
It is not necessary to select cell to perform task.

combo
 
amazing.. that works..

what i want to know is, h ow is that different from my code? why is there a problem if the cell is already selected? i mean.. if a cell is selected and you want to select it again.. it should just do it! not crash...


thanks!
 
Have you read my previous post ?
It explains you why your code crashed.
 


There's a differenc between Cells and Offset.

Offset CAN have arguments with a value of ZERO

Cells CANNOT!

Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
sorry.. yes i understand..

but i don't know why this crashes:

Code:
Function findfirstrow(ByRef wc As Long, ByRef gc As Long, ByRef y As Long, ByRef kf As Long) As Integer

    Dim temp As Integer
    temp = 16 * 5 * 3
    Cells(2, 1).Select
    Dim currow As Integer
    currow = ActiveCell.Row
    
    Do While (ActiveCell.Value <> wc)
        Cells(currow + temp, 0).Select
        currow = ActiveCell.Row
    Loop
    findfirstrow = currow     
End Function

it crashes on teh line:
Cells(currow + temp, 0).Select

thanks!
 
The culprit is highlighted:
Cells(currow + temp, [highlight]0[/highlight]).Select

Please, feel free to play with the F1 key ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
hehe.. yeah.. sorry.. i had forgotten about that line somehow.. i got it

thanks for your help.

the f1 key.. well.. they don't have help files installed here at work :( can you believe that?? lol... we had a discussion about that in one of my previous posts..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top