Consulting

Results 1 to 11 of 11

Thread: Solved: Using Select Case

  1. #1
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location

    Solved: Using Select Case

    I'm trying to determine how to use Select Case to selectively hide designated sets of non-contiguous columns. I am planning to create a user form that provides the user the option to click the appropriate button to create different views of a database, for example, "Auction View", "Kidding View", "Medical View" (and possibly more views to be added later). When the user selects the desired view, I am wanting all columns in the active worksheet (except a predefined set of columns) to be hidden.

    I'm not all that familiar with Select Case. I've seen examples used to automatically change one or more cell values based on the value of another cell. I can't seem to grasp conceptually how it would be used in the above described application.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,456
    Location
    We need a ton more info.

    What will be tested to determine what columns to show?

    What columns are showing for what?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    I don't think you need Select Case in this instance.

    From your description, the columns are either Hidden or not, thus offering only two choices. Select Case is used when multiple tests need to be made on a single parameter.

    So, read this link.They are using Togglebuttons, but they are either on or off, also.

    An example of Select Case would be if you wanted to test a value in the a textbox.

    [VBA]Select Case Me.Textbox1

    Case "ONE"
    'more code here

    Case "TWO"
    'more code here

    Case "THREE"
    'more code here

    End Select[/VBA]

    While you COULD use a Select Case for two tests, it's kinda overkill.

    David


  4. #4
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by xld
    We need a ton more info.

    What will be tested to determine what columns to show?

    What columns are showing for what?

    As I said, I just trying to conceptualize how an already unfamiliar process might work. With that in mind, I'm thinking that each button on the user form would call a script, let's call it "createView(myView)". For example, the Auction View button would call it thusly, createView("Auction") and should effectively hide all data columns except A, C, E, F. The Kidding View button, on the other hand, would call "createView("Kidding") and should hide all data columns except A, B, G, H, I.

    Once createView(myView) is called, it should respond based on which button was clicked.

    Does that make any sense?

  5. #5
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by Tinbendr
    I don't think you need Select Case in this instance.

    From your description, the columns are either Hidden or not, thus offering only two choices. Select Case is used when multiple tests need to be made on a single parameter.
    Thanks. I'm still learning.

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    It sounds like you might be trying to duplicate the functionality of the built in Custom Views.

  7. #7
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    It appears you re right. Perhaps a more simple solution would be to explore how to automate the Custom View functionality. Thanks for bringing that to my attention.

    Opv

  8. #8
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by mikerickson
    It sounds like you might be trying to duplicate the functionality of the built in Custom Views.
    Thanks again. That seems to do precisely what I want.

    Opv

  9. #9
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    OK. What might be the problem with following code? It is currently hiding ALL columns rather than leaving columns 1-12 visible.

    [VBA]
    Dim cols As Integer: For cols = 1 To gCols

    Select Case cols

    Case 1 - 12
    .Columns.Hidden = False
    Case Else
    .Columns.Hidden = True
    End Select

    Next
    [/VBA]

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,456
    Location
    [vba]

    Dim cols As Integer: For cols = 1 To gCols

    Select Case cols

    Case 1 - 12
    .Columns(cols).Hidden = False
    Case Else
    .Columns(cols).Hidden = True
    End Select

    Next
    [/vba]

    But you could simply use

    [vba]

    Columns(1).Resize(, 12).Hidden = False
    Columns(13).Resize(, gCols - 12).Hidden = True
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Thanks. Makes sense now that I see it. Some times after I stare at something so long, I overlook the obvious.

    Opv

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •