+ Reply to Thread
Results 1 to 8 of 8

Loop until 1 textbox in userform equals another

  1. #1
    Registered User
    Join Date
    08-22-2012
    Location
    Townsville
    MS-Off Ver
    Excel 2010
    Posts
    38

    Loop until 1 textbox in userform equals another

    Hi,

    I've got a start date in one textbox in a userform and an end date in another textbox in the userform.

    What I want to do is run the macro until the start date = the end date.

    Kind of thinking something like this..

    Please Login or Register  to view this content.
    Can this be done??

    Cheers,
    Amoxia

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,241

    Re: Loop until 1 textbox in userform equals another

    Yes. Actually with this type of "can this be done question" I suggest you try it!

    What you have is very close to the necessary code.

    Please Login or Register  to view this content.
    However, you do have a bug here. If TxtEndDate starts out being less than txtStartDate then you will have an infinite loop. Well, not truly infinite. You will get an error when txtStartDate reaches an overflow condition.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    08-22-2012
    Location
    Townsville
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Loop until 1 textbox in userform equals another

    Thanks 6StringJazzer for that.

    I did the following..

    Please Login or Register  to view this content.
    I get an error when I try to add a day to the start date. Any suggestions??

  4. #4
    Registered User
    Join Date
    08-22-2012
    Location
    Townsville
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Loop until 1 textbox in userform equals another

    Also if they are the same date it doesn't do 'my code.. I want it to run through the code once if they are the same date and then stop.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,241

    Re: Loop until 1 textbox in userform equals another

    This should solve both problems. However, I have not tested this code because I don't have your file.

    First, when you get an error, always provide the text of the error message.

    A textbox contains...wait for it...text. You can't do arithmetic on a string so you have to convert it to a number first. That's what CDate does--converts a string to a date. Then you add 1. Then you may need to convert it back to a string for display in the textbox--maybe not, I don't remember if it will convert it for you automatically. Even if it is converted for you, it may not be the format you want. So a call to Format will display exactly the format you want; just use the string to specify what you want it to look like. Here I used m/d/yyyy.

    Putting the Until condition at the end of the loop instead of the beginning will force the loop to execute at least once.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    08-22-2012
    Location
    Townsville
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Loop until 1 textbox in userform equals another

    Sorry, in future I will put the error message when I say there is an error.
    Thank you for your help.

    Just for others sake who read this in the future - this will not run the code once the start date = end date. At the start of the macro I just put the end date = end date + 1 so it would.

    Also some formatting issues with the date - in a couple of cases it must get confused with the dd/mm/yyyy format I have and thinks it is mm/dd/yyyy and it looks like the month changes not the day.

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Loop until 1 textbox in userform equals another

    wouldn't it be simpler to use
    Please Login or Register  to view this content.
    rather than looping?
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  8. #8
    Registered User
    Join Date
    08-22-2012
    Location
    Townsville
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Loop until 1 textbox in userform equals another

    Hi Mike,

    No I need it to loop to perform the macro as it puts a heap of data into the database as there are multiple teams.

    The date problem was because I didn't use CDate when extracting the value from the textbox in the macro. Fixed that now.

    Cheers,
    Amoxia

+ 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. Replies: 23
    Last Post: 07-12-2014, 07:09 PM
  2. TextBox Value equals worksheet...
    By SunRunnerNY in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-19-2013, 10:47 AM
  3. Userform Textbox updating a another userform textbox (im stuck!!)
    By wapwap in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-31-2011, 02:56 PM
  4. loop controls in userform except one textbox
    By gkisacik in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 03-28-2010, 04:30 AM
  5. For loop with Textbox in userform
    By Toadums in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-24-2008, 11:52 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