+ Reply to Thread
Results 1 to 7 of 7

Array Storage Problem: Array Elements Deleted on 'End' Command

  1. #1
    Registered User
    Join Date
    07-10-2014
    Location
    Red Deer, Canada
    MS-Off Ver
    2010
    Posts
    10

    Array Storage Problem: Array Elements Deleted on 'End' Command

    Hi guys,

    So I'm building an application for the company I work for. The application is quite large now, so I won't post the code, but I'll do my best to explain the issue.

    Basically, the application is set up so that users input most of the information directly into the cells of a spreadsheet. The type of information inputted is for HR purposes, and includes things like the role an employee will fill, their pay rate, phone number, etc. However, there is a second information gathering area the application uses. One column of the spreadsheet is labeled 'Safety Tickets,' and when a cell in this column is selected it triggers a user form to appear. This form is then used to collect a potentially large amount of additional information, (it was chosen over the alternative of cluttering up the spreadsheet). Once the user clicks the 'Confirm Inputs' button on the userform, the form's inputs are stored into a series of arrays (from now on referred to as the 'Safety Ticket Arrays') for the selected employee and the userform is unloaded. This form is then re-used each time a cell in the Safety Tickets column is selected for a different employee. Therefore, these Safety Ticket Arrays are updated dynamically with each new entry. The rest of the spreadsheet data, however, is not collected into arrays until a button is clicked and the application runs to completion.

    However, before I let the application run to completion, I have some validation checks on the data. If the user has forgotten to input a required field, or there are row gaps in the spreadsheet, then the application lets the user know what the issue is and ends the sub so that they can make the required fixes. To do this, I am using the 'End' command to prematurely exit the sub if validation check conditions are not met. The problem I'm having is that every time the code gets to one of these 'End' commands, all the data (elements) in the Safety Ticket Array's are deleted/emptied. This is obviously not a desired result, as the user now has to re-enter all that data. So my question is whether anyone knows a way around this issue? Perhaps there is an alternative to the End command?

    Here's some additional information:

    When the user clicks the button to run the main sub of the application, the subs are set up so that 1) Validation checks are done, 2) Data storage to arrays for all spreadsheet data - except Safety Ticket information - is done, (i.e., Safety Ticket data has already been collected into arrays previous to the main sub being run) 3) Array data is used to automatically fill out paperwork using MS Word templates, documents are saved to appropriate folders, and any required e-mails are sent.

    Currently, the data validation checks are done largely in the same sub as the array information gathering process, (so steps 1 & 2 are done in the same subroutine). I've done this because both processes require the application to first identify how many employee records have been created on the spreadsheet. Personally, I don't think this is an issue, since the arrays being affected (emptied) are the 'Safety Ticket' arrays which have been populated prior to the user running the main sub.

    I'd appreciate any input you might have. Please let me know if any additional information is needed!

    Thanks,
    Aiden

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Array Storage Problem: Array Elements Deleted on 'End' Command

    Tough to say, but if the data was entered into cells, then an array grabs them and clears the cells THEN checks to see if anything is missing, I'd say the logic process is in the wrong order.

    1) User fills in data
    2) data is checked for missing values and user prompted to complete them
    3) data is pulled from sheet into array
    4) ....etc.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    07-10-2014
    Location
    Red Deer, Canada
    MS-Off Ver
    2010
    Posts
    10

    Re: Array Storage Problem: Array Elements Deleted on 'End' Command

    Hi, thanks for the fast reply!

    The current logic process for non-safety ticket inputs is as you have stated: 1) user inputs data, 2) a validation check is performed 3) data is pulled from a sheet into an array. This process appears to be working fine.

    The issue is with the Safety Ticket Array's. I suppose I should have posted the logic process for them. Here it is: 1) User selects a cell in the 'Safety Ticket' column. 2) Code in worksheet identifies this, then checks the spreadsheet to find out how many employee records have been created so far. 3) validation checks: If no records started, end sub. If the cell selected has no adjacent data to it, then End sub. 4) Safety Ticket array's re-dimed/preserved for the number of employees. 5) code runs to find out which employee record (basically selection row number minus the first 6 rows that contain other data) has been selected, (variable selectedEmp is set). 6) Userform shown. 7) On clicking 'Confirm Inputs,' the Safety Ticket Arrays element dictated by the variable 'selectedEmp' is created/filled.

    Below is the worksheet code:

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 07-15-2014 at 09:58 AM. Reason: Added missing CODE tags. Please read and follow the Forum Rules, link above in the menu bar. Thanks.

  4. #4
    Registered User
    Join Date
    07-10-2014
    Location
    Red Deer, Canada
    MS-Off Ver
    2010
    Posts
    10

    Re: Array Storage Problem: Array Elements Deleted on 'End' Command

    Just checking to see if anyone has any more ideas today. I'd appreciate any help

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,444

    Re: Array Storage Problem: Array Elements Deleted on 'End' Command

    Don't use END.

    Note
    The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Code you have placed in the Unload, QueryUnload, and Terminate events of forms and class modules is not executed. Objects created from class modules are destroyed, files opened using the Open statement are closed, and memory used by your program is freed. Object references held by other programs are invalidated.
    Use Exit Sub if that is the action requried.
    Cheers
    Andy
    www.andypope.info

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

    To exit a sub use Exit Sub.
    If posting code please use code tags, see here.

  7. #7
    Registered User
    Join Date
    07-10-2014
    Location
    Red Deer, Canada
    MS-Off Ver
    2010
    Posts
    10

    Re: Array Storage Problem: Array Elements Deleted on 'End' Command

    Thanks guys!

    Yeah, I guess the 'End' command is true to its name and pretty much ends everything haha.

    My initial problem with using the 'Exit Sub' command was that since my application was so large, I had a lot of subroutine 'Calls' within a Main subroutine. Therefore, if the code reached an Exit Sub within one of these called subroutines, the application would just exit to the main sub, (which would then just call the next subroutine in the Main sub, which was not the desired result).

    I got around this issue with a fairly simple solution, with a variable I named 'exitSub'. Right before a line containing 'Exit Sub' coding was reaching, I coded for the exitSub variable to = 1. The application would then exit the sub to the Main sub, but before the next sub was called I had the program check whether exitSub equaled 1 with an If statement. If exitSub = 0, all was good and the program would continue. However, if exitSub = 1, another Exit Sub would result and the application would also exit the Main sub.

    Another option might have been to play around with the 'GoTo' command.

    I guess the solution wasn't all that complicated, but it sure had me pulling out my hair when my arrays kept emptying!

    Thanks again for everyone's help, I defiantly learned a lot about the End command with this trial!

+ 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. [SOLVED] Removing specific elements from an Array based on matching elements in a different Array
    By jamesdeuce in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-24-2014, 10:35 PM
  2. help assigning elements of dictionary to array and then pasting array into worksheet
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-05-2013, 10:37 AM
  3. [SOLVED] Populate Listbox with all rows of a dynamic array where elements of a single array match.
    By Tayque_J_Holmes in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-07-2012, 04:54 AM
  4. Problem assigning elements in row to array
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-11-2011, 08:06 AM
  5. [SOLVED] Count elements in array
    By Jason Morin in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-31-2005, 06:06 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