+ Reply to Thread
Results 1 to 8 of 8

Pass information from a userform to a module that then alters visible worksheet columns

  1. #1
    Registered User
    Join Date
    01-07-2014
    Location
    Surrey
    MS-Off Ver
    Excel 2010
    Posts
    23

    Question Pass information from a userform to a module that then alters visible worksheet columns

    Hi all,

    I am going around and around in circles and struggling a little bit with my query. I am barely proficient in VBA and much of my coding comes from collecting snippets of info from here and other forums. However, I am struggling with this one.

    I have a module that is looking at a few different worksheets in a workbook, taking a range of information from each and updating a worksheet that is used to produce a report (so it is designed to be aesthetically pleasing rather than useful for data recovery.)

    I have managed to get all the data onto the sheet, and I am happy with that - even if it is a bit slow.

    So then, I want a userform to pop up which has a whole bunch of checkboxes on. The user then selects which columns they want to display on their printed sheet and presses 'select'. My guess is that I need to make an array of the control names for each checkbox (because these corrolate to the headings of the sheet) and then pass these back to the original module which then hides the columns as required before printing it.

    userform sample.jpg

    this is my code on the userform:
    Please Login or Register  to view this content.
    and this is my module code:
    Please Login or Register  to view this content.
    Am I at least on the right track? For some reason the userform opens but after I press the button everything seems to fall over. I am not currently getting any errors, but the userform doesn't close and it doesn't seem to return anything to the module.

    Any guidance gratefully received.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: Pass information from a userform to a module that then alters visible worksheet column

    Can you post example file with userform and sheet with the headers involved ?
    For starters I would keep everything in 1 module namely the commandbutton module of the userform and work from there.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Pass information from a userform to a module that then alters visible worksheet column

    Sorry to say I cannot follow the logic of your code - on the face of it, populating the array should work but I have no idea what this is supposed to do...
    Please Login or Register  to view this content.
    Did you mean to hide the column there?

    An example of a generic form to show hide columns on a worksheet is attached. While the code is fairly simple, it uses WithEvents and a Class which may need explaining to you. Also, you'll notice, except for the button, there are no controls on the userform in design mode. There are all added when the userform is loaded.

    This was cobbled together in about 10 minutes so is fairly restrictive - it will handle up to 20 columns on the sheet that is active when the userform is loaded and the column headings are taken from row 1 only. It is just an example, after all.
    Attached Files Attached Files
    Last edited by cytop; 06-16-2016 at 07:47 AM.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Pass information from a userform to a module that then alters visible worksheet column

    I would recommend using a MultiSelect listbox instead of multiple checkboxes.

    This example code is for a userform with one listbox with checkboxes in its list and one command button.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Registered User
    Join Date
    01-07-2014
    Location
    Surrey
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Pass information from a userform to a module that then alters visible worksheet column

    @cytop - thanks for that - my apologies, I posted unfinished code. I have progressed this far (having realised that it was easier to take the false values from the the userform to hide, rather than the true ones which I want to see)...
    Please Login or Register  to view this content.
    Thank you for the sample worksheet - you are correct, it's not something I have used before, but I will have a play and see if it does what I want it to.

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: Pass information from a userform to a module that then alters visible worksheet column

    Another possibility if you want to keep your checkboxes.
    Name them Checkbox1, CheckBox2, etc...
    First UnHide all columns involved then with a simple loop through the controls you can hide columns you don't want to see.
    Please Login or Register  to view this content.
    Last edited by bakerman2; 06-16-2016 at 08:59 AM. Reason: Turned things around to shorten code.

  7. #7
    Registered User
    Join Date
    01-07-2014
    Location
    Surrey
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Pass information from a userform to a module that then alters visible worksheet column

    Ah yes, that might be a simpler option.

  8. #8
    Registered User
    Join Date
    01-07-2014
    Location
    Surrey
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Pass information from a userform to a module that then alters visible worksheet column

    I decided to go with bakerman2's suggestion. I hid the columns by cycling through the checkboxes on the userform and then kicked back to the module finish off - no array required. Might not be very clean, but does all that I need it to.

    Thanks for your help folks. cytop, I shall be looking into your code in more detail in the future in case it is useful for anything else.

+ 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. Update record - how to pass variable from one userform module to another
    By Rob K in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-11-2015, 09:49 AM
  2. Pass Variable From UserForm to Module
    By BLLMRC in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-24-2013, 12:41 PM
  3. [SOLVED] Can't pass values to module from a Userform (button)
    By Francisco Sousa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2013, 08:20 PM
  4. [SOLVED] Pass variables from userform to module
    By Bishonen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-08-2012, 07:54 AM
  5. Pass a simple value from custom UserForm to VBA module
    By MSieg in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-23-2012, 04:25 AM
  6. Pass values from UserForm ListBox/ TextBox to run the codes of a Module
    By SunOffice in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-05-2011, 08:59 AM
  7. [SOLVED] Pass variable from module to userform and back
    By ahundred25@yahoo.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-20-2006, 11:50 AM

Tags for this Thread

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