PC Review


Reply
Thread Tools Rate Thread

For control variable already in use

 
 
Otto Moehrbach
Guest
Posts: n/a
 
      25th Nov 2009
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 With Quote
 
 
 
 
Barb Reinhardt
Guest
Posts: n/a
 
      25th Nov 2009
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
>
> .
>

 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      25th Nov 2009
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


 
Reply With Quote
 
Otto Moehrbach
Guest
Posts: n/a
 
      25th Nov 2009
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 With Quote
 
Otto Moehrbach
Guest
Posts: n/a
 
      25th Nov 2009
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Referring to control value on form using variable name for control CompleteNewb Microsoft Excel Programming 5 1st Oct 2010 06:03 PM
How do I assign a control to a variable AS Control Wes from Scottsdale Microsoft Access VBA Modules 3 13th Oct 2008 04:00 PM
Using a variable to specify the name of a control lgbjr Microsoft VB .NET 8 30th Mar 2005 08:52 AM
Setfocus to a control where name of control saved in variable or field Andrew Microsoft Access Forms 4 26th Feb 2004 01:04 PM
using a variable to name a control AJ Microsoft Dot NET 4 7th Dec 2003 05:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:55 AM.