+ Reply to Thread
Results 1 to 19 of 19

Using an array in a case statment

  1. #1
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Using an array in a case statment

    Hi i have this code which is supose to populate a user form with data depending on the values the user enters.

    I originaly used this code but it was nearly 5000 rows of code because the form has 30 rows so i thought i would use an array to make things easier. The code worked fine but was a tad memory intensive.

    Original code x 30

    Please Login or Register  to view this content.
    Here is my new code - it complies fine but doesn't seem to work.

    Can some tell me where I am going wrong?

    Please Login or Register  to view this content.
    example of the table the code is looking at

    rotaexample.xlsx
    Last edited by nods; 11-17-2010 at 12:28 PM.

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Using an array in a case statment

    Doesn't work is not very helpful, doesn't give us much steer.

  3. #3
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: Using an array in a case statment

    i'm not sure whats wrong to be honest. It compiles fine. But the code doesnt seem to write and data to the spread sheet.

    even if i reverse engineer the code

    Please Login or Register  to view this content.

    it doesnt find data in the spread sheet. I think it is a problem with my case statment.

  4. #4
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: Using an array in a case statment

    If have this similar code using an array to write the data to the same table. and i get a syntax error on the

    Please Login or Register  to view this content.
    Any help would be really apreciated. I have been up all night looking at this,

    Please Login or Register  to view this content.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,997

    Re: Using an array in a case statment

    You need a space between the And and the underscore:
    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  6. #6
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: Using an array in a case statment

    Hi thanks for that but it didn't solve the problem.

    I have solved the syntax error in the above code though, But i am still having problems with first snippet of code.
    It compiles fone with out errors. But it doesn't work and I cant see what the problem is. Is there a way of testing my case statment? Or can anyone see where I am going wrong?

    Please Login or Register  to view this content.
    Many thanks

    Nods

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,997

    Re: Using an array in a case statment

    That was to solve your syntax issue, nothing else.
    Are you adding numbers together or concatenating text? If the latter, use '&' not '+'

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Using an array in a case statment

    It's no use using Select case if there is only one condition to test.
    Did you explore the differences between the operators + and & ?

    Please Login or Register  to view this content.



  9. #9
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: Using an array in a case statment

    Many thanks. I am concatenating text. I made the change you sugested and the code still doesn't work. My case statment now looks like:

    Please Login or Register  to view this content.
    I have attached an example of the table the code is looking at. It should find a match in column G and populate the form with columns L,M and N.

    Many thanks

    RotainputExample.xls

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,997

    Re: Using an array in a case statment

    That workbook might have been quite useful if it had your form in it...

  11. #11
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: Using an array in a case statment

    Sorry here is the work book with form. The only button I am concerned with is the 'View Historical Rota' button.

    RotainputExample.zip

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,997

    Re: Using an array in a case statment

    Your Case statement seems fine to me other than that this:
    Please Login or Register  to view this content.
    should be:
    Please Login or Register  to view this content.
    For future reference, most people find it easier to debug code if the project isn't password protected, and all the required sheets and forms are present for the code to run.

  13. #13
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: Using an array in a case statment

    Oops my bad. I was trying to upload the entire document but it kept failing.

    The password is donkey.

    I added the "" as you sugested and it made no difference.

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,997

    Re: Using an array in a case statment

    I've already unlocked and reviewed it, as I said, and your case statement works for me. What specifically do you mean when you say it doesn't work?

  15. #15
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: Using an array in a case statment

    So for example if the user selects Week 19 , Talpore and Cleaner then clicks View Historical rota the form should populate with that weeks data.And it doesn't.

  16. #16
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,997

    Re: Using an array in a case statment

    That's because this line stops it:
    Please Login or Register  to view this content.
    unless you have already altered the control values to something other than the defaults.

  17. #17
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: Using an array in a case statment

    Many thanks. That was obvious once you had pointed it out.

    The form seems to poulate fine now but only upto line 94. Fore example if was to select week 19, Talpore , cleaner it will populate perfectlty upto line 94(Employee N, On Friday). I was to select Restaurant it doesnt populate at all.

    I cant see how this could be possible?

  18. #18
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,997

    Re: Using an array in a case statment

    You only have data in column A for rows 1 to 93, and that's the column you use to determine the last row of data.

  19. #19
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: Using an array in a case statment

    Many thanks. Problem solved.

+ 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