+ Reply to Thread
Results 1 to 30 of 30

Private Sub UserForm_Initialize() Problem

  1. #1
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Private Sub UserForm_Initialize() Problem

    415COk5.png

    I am trying to get the userform to load the spreadsheet data into the right hand column entitled 'Account List'. By default when the form is loaded the data from the spreadsheet appears on the userform.

    Firstly, how can I define the width of each column showing in the list according to the length of the data in each?

    Secondly, I cannot figure out how to activate the modules to clear blank rows when the form is loaded before setting the dropdown options etc. It comes up with an error stating 'Compile error: Expected variable or procedure, not project'.

    Thirdly, how do I get the 'Account List' colum to auto-update when data is entered on the left hand side and saved to the spreadsheet?

    Non-functioning code:

    Please Login or Register  to view this content.
    Relevant Modules:

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

    Code in its entirety for Initialize:

    Please Login or Register  to view this content.
    Main module extended for reference:

    Please Login or Register  to view this content.
    Last edited by ThiaJay; 11-15-2014 at 04:49 AM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Private Sub UserForm_Initialize() Problem

    This is very interesting.

    So I will look at this for you.

  3. #3
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Private Sub UserForm_Initialize() Problem

    This is very interesting.

    So I will look at this for you.

    This code will display the Range A1 to E22 in Listbox2
    A1 to E1 will be displayed as headings.

    Please Login or Register  to view this content.

    This Code doubles the number in A2.
    It then refreshes the table in Listbox2.

    Please Login or Register  to view this content.
    This Code Will Auto Format the column Widths of Range A1 to E22
    It will use the data to modify the width of Listbox2
    It will also modify the widths of the columns to suit.

    Please Login or Register  to view this content.
    These two bits of code delete empty rows and columns:
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by mehmetcik; 11-15-2014 at 07:15 PM.

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Private Sub UserForm_Initialize() Problem

    Hi, Sheepdisease,

    you should consider to attach a workbook holding some data and the Form in order to make work easier for any helper.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

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

    Re: Private Sub UserForm_Initialize() Problem

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.
    If posting code please use code tags, see here.

  6. #6
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Private Sub UserForm_Initialize() Problem

    Thank you all for your replies, where do I insert the Listbox2 code you first mention? Is all of this supposed to be inserted into the initialize part of the code? Should anything you have quoted replace any of the existing code?

  7. #7
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Private Sub UserForm_Initialize() Problem

    If you post your spreadsheet with instructions.

    Then I can modify your code for you.

  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: Private Sub UserForm_Initialize() Problem

    mehmetcik

    Have you considered using Join for the column widths array?

    That's normally what I do.

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

    Re: Private Sub UserForm_Initialize() Problem

    This will adjust the column widths of your Listbox to the contents. It should be run after the list box is filled.

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  10. #10
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Private Sub UserForm_Initialize() Problem

    Thanks Norrie,

    I will adjust my code to use Join.

  11. #11
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Private Sub UserForm_Initialize() Problem

    Hello all and thank you for your help. The spreadsheet is attached as requested.

    UserForm for Export.xls

  12. #12
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Private Sub UserForm_Initialize() Problem

    Details of data entry and functionality were included in my previous post when you (very kindly) joined the thread.

  13. #13
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Private Sub UserForm_Initialize() Problem

    mehmetcik, where are you resident expert? I have answered your questions and provided the file you asked for. Thank you for your help in advance.

  14. #14
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Private Sub UserForm_Initialize() Problem

    Hi.

    I'm sorry for the delay.

    I had a look at your spreadsheet.

    You have not provided instructions on how you want it to operate.

    Please do that for me.


    By the way. You should have a toggle button for Search / Edit Mode.
    Last edited by mehmetcik; 11-19-2014 at 04:02 PM.

  15. #15
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Private Sub UserForm_Initialize() Problem

    Thank you so much for your reply

    What do you mean by toggle button for Search/Edit mode?

    The spreadsheet holds data in the sheet 'DataTracker', data is managed by using the userform to input and delete entries and at the moment the only way to scroll through the data by pressing the left and right navigation buttons towards the top. Typing in new information will automatically be added to the first blank row in the spreadsheet when the save button is pressed. The delete button should remove the currently displayed data row from the spreadsheet.

    I am trying to get the userform to load the spreadsheet data into the right hand column entitled 'Account List'. By default when the form is loaded the data from the spreadsheet appears on the userform.

    How do I get the 'Account List' colum to auto-update when data is entered on the left hand side and saved to the spreadsheet?

    I haven't figured out how to get the other two navigation buttons working to go to the very first and last data rows in the spreadsheet.

    I also want to be able to switch between rows by selecting something from the list appearing on the right hand side.

    Lastly, I want to be able to reorganise the data appearing on the left hand side (and spreadsheet) by either clicking and dragging and releasing the mouse button or if this is not possible/in addition by using buttons.

  16. #16
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Private Sub UserForm_Initialize() Problem

    Try this Spreadsheet.

    NB: I used another macro to help me write the Userform Subroutines.

    I am attaching that for you to look at too.
    Attached Files Attached Files

  17. #17
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Private Sub UserForm_Initialize() Problem

    Hello mehmetcik, well that's pretty impressive! Thank you so much

    I wonder, is there a way there can be a tick box to enable/disable to 'Are you sure you want to delete' prompt?
    Also, can switching between records be activated by default when editing as well as 'searching'?
    Is there a way to select a row in the 'Account List' column and by entering and saving data in the left hand side it puts the new data in the row below and moves all other data down to make space for this?
    What is the blank box below the 'Account List' column for?

    Finally, how do I get the sort buttons to move the current row up and down the spreadsheet?

  18. #18
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Private Sub UserForm_Initialize() Problem

    Hello mehmetcik, well that's pretty impressive! Thank you so much

    I wonder, is there a way there can be a tick box to enable/disable to 'Are you sure you want to delete' prompt?
    Also, can switching between records be activated by default when editing as well as 'searching'?
    Is there a way to select a row in the 'Account List' column and by entering and saving data in the left hand side it puts the new data in the row below and moves all other data down to make space for this?
    How do I get it to highlight the row in 'Account List' whose data is being displayed on the left hand side and update as scrolling through?
    What is the blank box below the 'Account List' column for?

    Finally, how do I get the sort buttons to move the current row up and down the spreadsheet?
    Last edited by ThiaJay; 11-23-2014 at 05:36 AM.

  19. #19
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Private Sub UserForm_Initialize() Problem

    if you change records during editing you will lose your data.

    Is that a risk you want to take?

    So you want to insert new data under the selected row?
    Presumably for keeping family records together.
    That is easily done.

    Highlighting the selected record in listbox,
    I tried that yesterday, I could not get a robust solution so I removed that.
    I will try again/

    I can use a togglebutton yo enable and disable prompts.

  20. #20
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Private Sub UserForm_Initialize() Problem

    Hello Mehmet,

    Thank you for your fast reply. I am prepared to take that risk, could always have a prompt come up in instances where data has been entered but not saved, allowing the user to decide whether to save or lose progress before switching records.

    You have the right idea with all of this. New data under the selected row would be best, if we can get the 'sort' buttons working then it would be possible to move the data if it were inserted in the wrong place. Moving data around should happen when the button is pressed and this change should be made permanently by automatically saving the change in the spreadsheet.

    Toggle button would be great, perhaps a menu item could be introduced, like 'Preferences'?

    It would be great if highlighting could be sorted.

    Thank you so much for all your help.

  21. #21
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Private Sub UserForm_Initialize() Problem

    Try this version.
    Attached Files Attached Files

  22. #22
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Private Sub UserForm_Initialize() Problem

    Hello mehmet, that function seems to be working well, thank you!

    Can you please help to get the 'sort' functionality working to organise the data showing in the account list?

    Can you please enable amendments during 'search' mode and have a prompt come up in instances where data has been entered but not saved, allowing the user to decide whether to save or lose progress before switching records? The arrows should also allow the user to view the first/next/previous/last record with the same prompt appearing if there is unsaved data.

    Can the arrows for changing the data currently being viewed also be enabled in all instances?

    Is it possible to make the 'Enable Prompts' button into a menu item, which would appear under 'Preferences' in a drop down like in a normal application?

  23. #23
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Private Sub UserForm_Initialize() Problem

    You need to specify what you want the sort function to do.

    Meanwhile.

    This is a revised userform
    Attached Files Attached Files
    Last edited by mehmetcik; 11-25-2014 at 04:31 PM.

  24. #24
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Private Sub UserForm_Initialize() Problem

    Thank you for your fast reply.

    The sort buttons should move the current selected row up and down the spreadsheet and up and down the list, with the move being saved each time to maintain its new position (without a prompt).

  25. #25
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Private Sub UserForm_Initialize() Problem

    Ok

    I've added some additional functions.
    Attached Files Attached Files

  26. #26
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Private Sub UserForm_Initialize() Problem

    Hello there, thank you for the update. The sort functionality appears to work well. The 'Find Entry' button appears to wipe the 'Account List' from view, what is that button supposed to do?

  27. #27
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Private Sub UserForm_Initialize() Problem

    Thank you for dealing with the prompt too, which also works well. When there is information entered but not saved, is it possible for it to ask 'You are about to move to the next row, would you like to save your new entry?' and then Okay would save and Cancel would move to the next row without saving progress.

  28. #28
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Private Sub UserForm_Initialize() Problem

    ok.

    press clear to clear your screen,

    then type something into one of the first three text boxes

    Like joan into text box 1

    now press find.

    it will list all entries that contain the text joan ie Joanne,

    change the text in textbox2 or textbox3

    ie f in textbox2 will reduce your options to two entries.

    Click on the entry in the listbox to load it and return to the normal screen.



    Next click on textbox 6 which contains the email address.
    This should be self explanatory.
    Attached Files Attached Files
    Last edited by mehmetcik; 11-29-2014 at 02:24 AM.

  29. #29
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Private Sub UserForm_Initialize() Problem

    Hello Mehmet,

    Thank you for the update. I think it is working quite well but there seems to be a strange behaviour when moving between the last few entries when using the previous and next buttons with regards to it coming up with a prompt even if the data has been saved. Have you seen this? It's quite strange.

    Also, can the buttons you have introduced be changed to menu items like in this example? http://www.jkp-ads.com/downloadscrip...erFormMenu.zip

    It would be good to have a menu entitled 'Preferences' and under that menu have the functionality of the buttons turned on and off by selecting the option from the menu dropdown which would display a tick when activated and not when disabled.
    Last edited by ThiaJay; 12-02-2014 at 03:45 PM.

  30. #30
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Private Sub UserForm_Initialize() Problem

    Hello Mehmet, I imagine that you are really busy, just wanted to check that the points previously raised were still on your to-do list?

+ 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. Private Sub UserForm_Initialize() not run
    By Reemet in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-29-2014, 01:54 PM
  2. Private Sub Worksheet_Change problem
    By Delorean14 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-13-2013, 02:25 AM
  3. [SOLVED] Private messages problem - I think
    By stojko89 in forum Suggestions for Improvement
    Replies: 2
    Last Post: 09-07-2012, 05:12 AM
  4. call Private Sub A_Click from UserForm_Initialize
    By TMP123 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-28-2010, 08:55 AM
  5. Copy Problem-Private sub userform_activate()
    By Syed Haider Ali in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2006, 04:30 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