Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default For control variable already in use

Excel 2007, Win 7 64-bit
I have the following code. Note that this is a self-standing macro. This
macro is not called by any other macro. It is executed by a Forms button.
There are 3 FOR loops, none nested

Sub SortByName()
With Sheets("SortSht")
Copy & Paste
Set rColA =...................
For cc = rColA.Count To 2 Step -2
Stuff
Next cc
For cc = rColA.Count To 1 Step -1
Stuff
Next cc
For cc = rColA.Count To 1 Step -1
Stuff
Next cc
End With
End sub

I get an error with the bottom FOR loop. Error reads "For control variable
already in use". I change the variable in that FOR loop to "c". I get the
same identical error but now in the second FOR loop. What is wrong with
this code? Thanks for your time. Otto

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default For control variable already in use

Have you declared all of your variables?

Barb Reinhardt



"Otto Moehrbach" wrote:

Excel 2007, Win 7 64-bit
I have the following code. Note that this is a self-standing macro. This
macro is not called by any other macro. It is executed by a Forms button.
There are 3 FOR loops, none nested

Sub SortByName()
With Sheets("SortSht")
Copy & Paste
Set rColA =...................
For cc = rColA.Count To 2 Step -2
Stuff
Next cc
For cc = rColA.Count To 1 Step -1
Stuff
Next cc
For cc = rColA.Count To 1 Step -1
Stuff
Next cc
End With
End sub

I get an error with the bottom FOR loop. Error reads "For control variable
already in use". I change the variable in that FOR loop to "c". I get the
same identical error but now in the second FOR loop. What is wrong with
this code? Thanks for your time. Otto

.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default For control variable already in use

Hi Otto,

Without seeing what code you have in "Stuff' I am guessing that you might
have got something like an End With and Next out of order somewhere or maybe
another For/Next and have mixed up their order by having Next addressing the
wrong variable. eg. Next cc where next c should be.

If you can't find the problem then can you post all of your code.

--
Regards,

OssieMac


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default For control variable already in use

Barb & Ossie
Yes, I declared all my variables and have "Option Explicit". The
"Stuff" is simple copy/paste code with no For loops or Withs/End Withs.
That is what is so perplexing about this. The entire macro is a very simple
macro consisting of nothing more than moving data around. Thanks for your
time. Otto

"Otto Moehrbach" wrote in message
...
Excel 2007, Win 7 64-bit
I have the following code. Note that this is a self-standing macro. This
macro is not called by any other macro. It is executed by a Forms
button. There are 3 FOR loops, none nested

Sub SortByName()
With Sheets("SortSht")
Copy & Paste
Set rColA =...................
For cc = rColA.Count To 2 Step -2
Stuff
Next cc
For cc = rColA.Count To 1 Step -1
Stuff
Next cc
For cc = rColA.Count To 1 Step -1
Stuff
Next cc
End With
End sub

I get an error with the bottom FOR loop. Error reads "For control
variable already in use". I change the variable in that FOR loop to "c".
I get the same identical error but now in the second FOR loop. What is
wrong with this code? Thanks for your time. Otto


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default For control variable already in use

Found it!!!
I wrote this code for an OP. He was having this problem. I found out this
morning that I was NOT having this problem with the same file. I sent him
mine and all is well. I figure his code got corrupted. Thanks again. Otto

"Otto Moehrbach" wrote in message
...
Excel 2007, Win 7 64-bit
I have the following code. Note that this is a self-standing macro. This
macro is not called by any other macro. It is executed by a Forms
button. There are 3 FOR loops, none nested

Sub SortByName()
With Sheets("SortSht")
Copy & Paste
Set rColA =...................
For cc = rColA.Count To 2 Step -2
Stuff
Next cc
For cc = rColA.Count To 1 Step -1
Stuff
Next cc
For cc = rColA.Count To 1 Step -1
Stuff
Next cc
End With
End sub

I get an error with the bottom FOR loop. Error reads "For control
variable already in use". I change the variable in that FOR loop to "c".
I get the same identical error but now in the second FOR loop. What is
wrong with this code? Thanks for your time. Otto




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable Control name Brett Excel Programming 6 May 15th 09 07:29 AM
Variable control tip text Steve Excel Discussion (Misc queries) 3 June 30th 05 03:48 PM
variable form control Roman[_4_] Excel Programming 2 June 15th 05 08:20 AM
using variable for a control name mark kubicki Excel Programming 1 April 3rd 04 12:31 AM
Variable Control Names Garry Jones Excel Programming 4 October 3rd 03 10:45 PM


All times are GMT +1. The time now is 06:34 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"