+ Reply to Thread
Results 1 to 19 of 19

For Next Loop Problem

  1. #1
    Registered User
    Join Date
    10-14-2011
    Location
    California, USA
    MS-Off Ver
    2007
    Posts
    17

    For Next Loop Problem

    Hi. I'm trying to use this code to update records in a worksheet using a user form. When I run this, I get a message saying Invalid Next Control Variable Reference and currentRow (after Next) is highlighted. I declare currentRow as Long globally. Can anyone steer me in the right direction. I did a crappy job of naming my controls, but I'm learning as I go.



    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: For Next Loop Problem

    You've misspelled currentRow here.
    Please Login or Register  to view this content.
    PS To avoid this sort of thing you can add Option Explicit at the top of the module.
    If posting code please use code tags, see here.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: For Next Loop Problem

    Hi,

    What is TXTInvoiceLookup.Text ?

    Does this happen at the first attempted pass through the loop or later?

    It would help if you were to upload the actual workbook and explain to us how you use it.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,912

    Re: For Next Loop Problem

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  5. #5
    Registered User
    Join Date
    10-14-2011
    Location
    California, USA
    MS-Off Ver
    2007
    Posts
    17

    Re: For Next Loop Problem

    Gosh...I missed that, and that fixed the problem. I'll google option explicit to learn about that.

    Thank you Norie, and have a great weekend!

  6. #6
    Registered User
    Join Date
    10-14-2011
    Location
    California, USA
    MS-Off Ver
    2007
    Posts
    17

    Re: For Next Loop Problem

    Hi Richard. TXTInvoiceLookup is the text box where the user enters the invoice number they want to find and possibly edit. Norie found the problem, so I want to thank you for taking time to look at this, but I don't want to waste your time by looking at the entire workbook...it seems to be working fine now.

    Thank you again,
    Guy

  7. #7
    Registered User
    Join Date
    10-14-2011
    Location
    California, USA
    MS-Off Ver
    2007
    Posts
    17

    Re: For Next Loop Problem

    Thanks bakerman2 - you know as I was assigning all those variables it occurred to me that it seemed strange that I couldn't just use the value in the control. That's just the way I learned from Exceltrainingvideos.com. There is probably an application for that approach, but for me, this is much cleaner. Thank you very much! I'll plug this in and although the users will never know the difference, I will.

    Thanks again,
    Guy

  8. #8
    Registered User
    Join Date
    10-14-2011
    Location
    California, USA
    MS-Off Ver
    2007
    Posts
    17

    Re: For Next Loop Problem

    This is a separate problem, but it is related to the search I received help for above. My form has a text box "TXTInvoiceLookup" that the user will input an invoice number to look up into. Then, the form will populate all controls with values from that invoice. Once the form is loaded with data, the user can edit and then save the record (the update from my first post). So, the user opens the form, types in an invoice number, clicks search, and if the invoice number is found, the form fills with data. Then they edit and click Update, and the new data is written into the worksheet. Everything seems to work ok as long as I provide a valid number. If I don't, nothing seems to happen, and instead, I'd like to have a message box alert the user to try a different number. I've tried a couple of things that didn't work, so here I am again...Here's the code for the search button: And sorry if I should have started a new thread, I thought it would be helpful to have ready access to the update code as well. And again, thank you in advance for taking your time to read this and fix my crappy code.

    Please Login or Register  to view this content.

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: For Next Loop Problem

    There's really no need to loop.

    Try this.
    Please Login or Register  to view this content.

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,912

    Re: For Next Loop Problem

    @ gyster.
    If Invoicenumber is unique in column A then you don't have to loop every value but you can do with this.
    Please Login or Register  to view this content.
    PS thanks for the rep.

    @ Norie

    When using With ... you have forgotten to use
    Please Login or Register  to view this content.
    otherwise it's still the active sheet where the search is.
    Please Login or Register  to view this content.
    Last edited by bakerman2; 07-10-2016 at 02:34 AM.

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: For Next Loop Problem

    Oops, I did miss the dots.

    Not sure how it affects this though.
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    10-14-2011
    Location
    California, USA
    MS-Off Ver
    2007
    Posts
    17

    Re: For Next Loop Problem

    First, a quick thank you to Norie and bakerman2 for the help and lessons. Unfortunately neither solution is working just yet. bakerman2, if I understand the code this line
    Please Login or Register  to view this content.
    searches column 1 starting at the first row for the value the user enters in TXTInvoiceLookup. Then, if a match is found, it appears the next bit writes values to the worksheet. This is where I'm getting confused, and it's likely because I combined these two topics. I noticed your code is for the BtnUpdate sub. I've got the search function on a separate button so the user can look for an invoice first, then edit any values needed, and then use the update routine to write the records to the worksheet. Of course if I've misinterpreted what this does
    Please Login or Register  to view this content.
    then this doesn't make any sense. So when I run this, I get an error (Run Time Error 13 - Type Mismatch) on the fRow =line. The invoice numbers in column one start on row 4 and they are unique.

    Norie, when I try the code you modified, I get the error message even if I enter a valid number. I'm not sure where to go from here. I think I understand what you've got there - apparently
    Please Login or Register  to view this content.
    searches all values in Inv History column 1 for the number stored in InvoiceNumber, and if a match isn't found, the error message is displayed, otherwise it populates the form with values from the row containing the value in InvoiceNumber. So, why no joy?

    I cannot thank you all enough.

    Guy

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: For Next Loop Problem

    Guy

    What type of data do you have in the first column? Text? Numeric? Alphanumeric?

  14. #14
    Registered User
    Join Date
    10-14-2011
    Location
    California, USA
    MS-Off Ver
    2007
    Posts
    17

    Re: For Next Loop Problem

    Hi Norie. It's all numbers and the cell is formatted as Number with 0 decimals.

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: For Next Loop Problem

    I think it might be time to see a workbook.

    Any chance you could upload a sample file?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  16. #16
    Registered User
    Join Date
    10-14-2011
    Location
    California, USA
    MS-Off Ver
    2007
    Posts
    17

    Re: For Next Loop Problem

    Hi Norie. Sorry this is taking so much time. Here's the workbook.
    Attached Files Attached Files

  17. #17
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,912

    Re: For Next Loop Problem

    A lot of your problems will be solved if you declare Invoicenumber correctly.
    Since they are true numbers you can't declare them as String but as Double.
    So codes become this.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    10-14-2011
    Location
    California, USA
    MS-Off Ver
    2007
    Posts
    17

    Re: For Next Loop Problem

    That was exactly it! I very much appreciate the help you and Norie provided. Many thanks.

    Sincerely,
    Guy

  19. #19
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,912

    Re: For Next Loop Problem

    You're welcome and thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Loop problem
    By HeHeHaHa in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-15-2014, 08:43 AM
  2. [SOLVED] Problem with loop
    By Dedaluss in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-25-2014, 12:17 AM
  3. problem with for next loop
    By ravi007008 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-22-2014, 12:09 PM
  4. Vlookup problem in a loop with cell property and variable cell problem (long title sry)
    By ExcelsiorLux in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-13-2013, 10:38 AM
  5. [SOLVED] Loop inside a loop problem!
    By questionguy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-22-2012, 12:54 PM
  6. loop's problem
    By eran3185 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-10-2007, 04:30 AM
  7. Problem adding charts using Do-Loop Until loop
    By Chris Bromley in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-23-2005, 09:06 AM

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