Combo Box Control with VBA

marka87uk

Board Regular
Joined
Mar 24, 2007
Messages
247
Hi,

Firstly, if Mr Excel himself reads this, then I'd like to say I really enjoy the Guerilla Data Analysis book! :)

Secondly, and on-topic, I wish to use a Combo Box to select columns to hide or show.

For instance, in columns C-F I have Period1, G-J, Period2, K-N, Period3, and so-on.

I wish to be able to select a period from the Combo Box, and then hide or show the columns as needed depending on the value.

I'm very new to VBA (but not totally new to VB), but could someone explain to me how to go about doing this?

I'm using Excel 2003, where I see there are two Combo Box's under the Forms and the Control Toolbox toolbars. :)
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Well! I have to say, I cracked it myself! :)

I don't know if this is the best or most correct method, but feel free to advise me please!

Code:
Private Sub Workbook_Open()
CreatePeriods
End Sub

Sub CreatePeriods()
    With Sheet1.SelectPeriod
        .AddItem "Period 1"
        .AddItem "Period 2"
        .AddItem "Period 3"
    End With
End Sub

Code:
Private Sub SelectPeriod_Change()
Columns("C:N").Select
Selection.EntireColumn.Hidden = True

    If SelectPeriod.Value = "Period 1" Then
        Columns("C:F").Select
        Selection.EntireColumn.Hidden = False
    End If
    
    If SelectPeriod.Value = "Period 2" Then
        Columns("G:J").Select
        Selection.EntireColumn.Hidden = False
    End If
    
    If SelectPeriod.Value = "Period 3" Then
        Columns("K:N").Select
        Selection.EntireColumn.Hidden = False
    End If
End Sub

It works exactly how I wanted! :LOL:
 
Upvote 0
Mark

Glad to see you've got it working yourself.:)

One point though, you don't need to select.
Code:
Private Sub SelectPeriod_Change()
    Columns("C:N").EntireColumn.Hidden = True

    Set rng = Columns("C:F")
    
    rng.Offset(, SelectPeriod.ListIndex * 4).EntireColumn.Hidden = False
    
End Sub
 
Upvote 0
Ah, that simplifies it a bit! Thanks :)

Out of interest, what does the variable you used, rng, stand for?
 
Upvote 0
It doesn't really 'stand' for anything.

What I'm doing is creating a reference to an object, specifically the range object columns C:F.

I really should have declared the variable like this.:oops:
Code:
Dim rng As Range
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,011
Members
448,935
Latest member
ijat

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top