+ Reply to Thread
Results 1 to 10 of 10

compile error: For control variable already in use

  1. #1
    Forum Contributor
    Join Date
    02-20-2009
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    467

    compile error: For control variable already in use

    I have the code below behind a command button on a worksheet and I have tried adding the bit highlighted in red to the beginning but when I try running the code now I get a 'compile error' "For control variable already in use" and when I debug it highlights the text "For i = 4 To lrow" (highlighted in red). Can anyone help with what I need to change to make this work? Please let me know if it needs further clarification of what it is supposed to be doing.

    I also want to add some code to the macro where I have highlighted in green further down. At this point in the macro I want some code that deletes rows where the value in column P is the same as column BA. Any help here would be greatly appreciated too.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: compile error: For control variable already in use

    You can not use i inside a For next loop that your already using i. You need another variable Ex: ii

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Forum Contributor
    Join Date
    02-20-2009
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    467

    Re: compile error: For control variable already in use

    Thanks for your reply Mike but I'm not sure I follow how to change my code? Could you possibly post how exactly I need to change my code?

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: compile error: For control variable already in use

    Where you have Highlighted your Code in Red the Second For i = needs to be a different variable. Change it to (ii or n or r) but it cannot be (i) because its inside your First For i = loop.

  5. #5
    Forum Contributor
    Join Date
    02-20-2009
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    467

    Re: compile error: For control variable already in use

    Thanks for your reply Mike. I am partly understanding you but I have made the change you mentioned but now getting a new compiler error: "Invalid Next control variable reference" and it highlights the line "GetNext" highlighted below. So, here is now the code I have got (up to 'Delete rows where price has not changed') with what I have changed/added highlighted in red.

    Please Login or Register  to view this content.

  6. #6
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: compile error: For control variable already in use

    Only thing I can see off the bat is you dont have a Next ii.I dont know exactly want your doing but try adding a next ii like this below

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    02-20-2009
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    467

    Re: compile error: For control variable already in use

    ok, done that and now I am getting another compile error: "Next without For" and it highlights the word "Next" in the line

    Please Login or Register  to view this content.

  8. #8
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: compile error: For control variable already in use

    Again at just looking at your code and not knowing what exactly your doing, you need to make sure anywhere you use With Worksheet you have a end with.
    Try changing adding a End with above the Getnext

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    02-20-2009
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    467

    Re: compile error: For control variable already in use

    Not getting any errors when running the code now but doesn't do what it is supposed to now

    This is becoming difficult to explain and for you to fully understand without posting an example and a more detailed explanation. Give me a bit of time to prepare a version of my workbook that hides some of my more important work which I don't want to post on to the internet for confidentiality reasons.

    It may get tomorrow as I need to go somewhere right now. Thanks for your efforts so far though, hopefully we can get to the bottom of it soon!

  10. #10
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: compile error: For control variable already in use

    Ok sounds good. Post the workbook and give details of your expected output.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1