+ Reply to Thread
Results 1 to 34 of 34

Do/Loop Until using variables declared

  1. #1
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Do/Loop Until using variables declared

    How do I structure a Do/Loop to loop all rows starting from row 3? I declared a variable for the rows (intRegion), but I don't know how to make the loop run through all of the rows without typing in the input boxes by hand. My rows are sorted by state, but I would like it to loop for all of one state, save the file, and then repeat the same process for all other states.


    Please Login or Register  to view this content.
    Thanks for the help!!

  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: Do/Loop Until using variables declared

    How will VBA be able to tell when the data for a state ends and the data for the next state starts?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Thanks for your reply. That's what I've been having trouble with. My idea is to loop until, for example, cell A3 <> A4. And when that happens, I'd like it to save as a new file and then continuing doing the same thing with the next state.

    As of now, I've been using an input box to tell vba when to start and stop, but that's not too efficient if I need to run the code multiple times.

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

    Re: Do/Loop Until using variables declared

    Can you upload an example workbook?

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

  5. #5
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Hi,


    I'm not able to upload workbook, but hopefully this may look a little better to show the structure of my code...Sorry for the inconvenience

    I have declared all of my variables but I just have an issue of how to use them with a Do Loop Until statement

    Please Login or Register  to view this content.
    intState to intLast are looking at rows 14 to 17, but is there a way to make it loop through all of my rows using a Do Until Loop with having to define the rows I want it to look at (so essentially getting rid of the variables, intState and intLast)? Or is there a better way for me to look at this? The bold in my code shows what I would like to perform.

    Thanks!
    Last edited by LLL0422; 06-04-2014 at 09:05 AM.

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

    Re: Do/Loop Until using variables declared

    You could use a loop to go through all the rows but without seeing how your data is structured it's hard to give specifics.

    Even knowing what column the state is in would help.

  7. #7
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Sorry, the states are listed in the 3rd column, and the rows are sorted by state. So ideally, I'd like the code to loop until the state changes, save the template, and then go through loop again until state changes, save the template, etc. I hope that helps... Initially I had an input box which was only able to loop one state at a time, but I hit an obstacle when I tried to loop through multiple states because the code couldn't recognize when the state changes.
    Last edited by LLL0422; 06-04-2014 at 09:18 AM.

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

    Re: Do/Loop Until using variables declared

    All you really need is a loop to go through all the rows.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Thanks so much! So what I am filling out used to follow this code:

    Please Login or Register  to view this content.
    Would it be correct to have it like this?:

    Please Login or Register  to view this content.
    Because I am pulling info from each row and then moving to the next row, is rState able to specify what row I'm using?

    Also, I am getting runtime error 1004: "Method 'Range' of 'object' '_Global' failed" for the "For Each" code

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

    Re: Do/Loop Until using variables declared

    Sorry I'm not really sure what you are asking - for example, what's rState?

    In the code I posted cl is the cell that has the state, you can get it's row using cl.Row.

  11. #11
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Sorry, instead of cl, I changed it to rState.

    Is this what you mean by cl.Row? And is this okay to specify what row it should look at?

    Please Login or Register  to view this content.
    Sorry for confusion, I'm super new at VBA and am still trying to find my way to understand what to do.

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

    Re: Do/Loop Until using variables declared

    That would do it, though you could also use this.
    Please Login or Register  to view this content.
    This tells VBA to put the value that is offset 6 columns to the right of rState into dtEnd.

    So if rState was referencing C10, rState.Offset(,6) would reference I10.

    By the way, when using Cells you can use either the letter or number to specify the column.

    So you could use this.
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Good to know!

    Also within my "For each" loop, I am also looping what I'm calling intPlan, which loops from columns 12 to 35.

    Is this okay to specify the columns?:

    Please Login or Register  to view this content.
    And is there a way to fix the range error that i get from this?:
    Please Login or Register  to view this content.

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

    Re: Do/Loop Until using variables declared

    If that gives you the right columns then that should be fine.

    The error is my fault, the parentheses aren't closed properly, Row shouldn't be there and no sheet is referenced.

    I'm not quite sure which sheet you are working with but if it's Sheets(strSheet) then you could try something like this.
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Ah okay, I'll remove Row from it.

  16. #16
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    The loop works great for the first row, but is there a way to make it not open my template again when its populating for the other rows?
    Also, the loop for "intPlan" also keeps recognizing that intRowKnt = 14 when its reading a new row, but it should be continuing where it left off.

    Please Login or Register  to view this content.
    Last edited by LLL0422; 06-04-2014 at 11:35 AM.

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

    Re: Do/Loop Until using variables declared

    There's no code that's 'opening' the template, but there is this,
    Please Login or Register  to view this content.
    which activates the template.

    There's no need to activate a workbook/worksheet to copy/paste data.

    I would have something like this before the loop, perhaps even right at the top of the code.
    Please Login or Register  to view this content.
    Then you can use something like this.
    Please Login or Register  to view this content.
    By the way, I removed this because, as far as I can see, it wasn't really doing anything.
    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    It seems like
    Please Login or Register  to view this content.
    is giving me a mismatch error for some reason..

    Please Login or Register  to view this content.
    Last edited by LLL0422; 06-04-2014 at 11:55 AM.

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

    Re: Do/Loop Until using variables declared

    Definitely a type mismatch error and not a subscript out of range?

  20. #20
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Yep, definitely Type mismatch.

    Please Login or Register  to view this content.
    This is the code that I have so far. Hope this helps.
    Last edited by LLL0422; 06-04-2014 at 12:07 PM.

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

    Re: Do/Loop Until using variables declared

    Sorry I can't see why that's happening, it works fine for me.

    How have you declared strTemplate?

  22. #22
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Could there be something wrong with my code?

    Please Login or Register  to view this content.

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

    Re: Do/Loop Until using variables declared

    The problem could be that strTemplate doesn't actually have a value until a little bit further down in the code.

    By the way, this won't work - see the code in post #17.
    Please Login or Register  to view this content.

  24. #24
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Ah, so should it look something like this?

    Please Login or Register  to view this content.

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

    Re: Do/Loop Until using variables declared

    No, more like this.
    Please Login or Register  to view this content.
    Or even this.
    Please Login or Register  to view this content.
    By the way, which worksheet/workbook is column C in here?
    Please Login or Register  to view this content.

  26. #26
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    It is in the IDFile workbook. Template is a separate workbook.

    Ah now it says out of range if I put it after declaring what strTemplate is.

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

    Re: Do/Loop Until using variables declared

    Is the template workbook open?

    This code will refer to column C in the active sheet, whatever that is.
    Please Login or Register  to view this content.
    As will all the ranges in this section of code.
    Please Login or Register  to view this content.

  28. #28
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    The code should open the template workbook.

    My issue was that when it loops the plans, it would keep trying to reopen the template.
    How do I condition the code so that it only opens once per state?

    The code used to be:

    Please Login or Register  to view this content.
    Last edited by LLL0422; 06-04-2014 at 12:38 PM.

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

    Re: Do/Loop Until using variables declared

    That wasn't in the earlier code where you mentioned the template being opened again.

    Try putting this at the top of the code.
    Please Login or Register  to view this content.
    It might also be an idea to create a reference to the IDFile workbook

    For example, if the IDFile is open.
    Please Login or Register  to view this content.
    Then you can make sure you are referring to the correct workbook and worksheet here.
    Please Login or Register  to view this content.

  30. #30
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    Sorry, I made some adjustments and then posted the code that I had at the moment. I will try what you suggested.

  31. #31
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    I found a different approach where I can use a function to test if the sheet is already open and it seems to work! But I did Dim wbTemplate as Workbook also.

    One last thing..hopefully,

    Please Login or Register  to view this content.
    C3: AL
    C4: AL
    C5: FL

    This works great, except that once the code recognized that C4 <> C5, it goes straight to saving the file rather than completing the template with C4's information.

    Thank you for bearing with me!!

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

    Re: Do/Loop Until using variables declared

    Is there no way you could mock up an example data workbook and template and attach them here?

    That would make it a lot easier to give help with the code.

    As it is I'm going on the code you are posting.

    PS If the whole point of the code is to split up the data by state then there could be other(better?) methods.:l

  33. #33
    Registered User
    Join Date
    05-28-2014
    Posts
    67

    Re: Do/Loop Until using variables declared

    I'm really sorry, I can't upload an example workbook at the moment.

    As for my data, all of the states have to be on the same worksheet, and I need to somehow make it create a separate template for each state all at once. Originally, I would run the code state by state, but for time's sake, that's not too efficient. Sorry for trouble!

    I think I've gotten most of my questions answered, but I will make new thread for other questions not related to this one.

    Thanks so much for your help! I really appreciate it!

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

    Re: Do/Loop Until using variables declared

    This should actually be a very straightforward task and you'll find plenty of examples if you search the forum.

+ 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. Problem with public global declared variables
    By vonb3ta in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-03-2013, 07:20 AM
  2. Macro for autofilter using variables declared in worksheet
    By Dave Peterson in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-06-2005, 07:05 AM
  3. Macro for autofilter using variables declared in worksheet
    By Jeff in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 AM
  4. Macro for autofilter using variables declared in worksheet
    By Jeff in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  5. [SOLVED] Macro for autofilter using variables declared in worksheet
    By Jeff in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-13-2005, 11:05 PM

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