Display/Hide # of columns based on user input

mrMozambique

Board Regular
Joined
Mar 9, 2005
Messages
97
Hi all. This may be around somewhere in the forums, but I appear to be searching incorrect key words.

I have a range of columns (I:AQ) that a user can freely use to enter quantitative data. Each column represents a province of Vietnam. Column AR contains qualitative data (comments) that can be used to explain the quantitative data in columns I:AQ. The large majority of users will use only a few columns. However, I needed to open it to 35 columns to accommodate a small group of users that work in many provinces.

I'd like to create a combobox or validation list in a cell where users select the number of provinces they work in and then run a macro to display the number of columns the user selects. For example, if the user selects 10 provinces, columns I:R will be displayed and S:AQ are hidden.

I'm sure this is possible, but I'm only an beginner/intermediate VBA programmer. Any suggestions?

Thanks in advance.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Something along the lines of

Columns("I:aq").Select
Selection.EntireColumn.Hidden = True
mynum = Application.InputBox("Enter a number")
Range("I1").Select
numRows = Selection.Rows.Count
numColumns = Selection.Columns.Count
Selection.Resize(numRows + 0, numColumns + mynum).Select
Selection.EntireColumn.Hidden = False
 
Upvote 0
Thanks, that worked great. Here's what I finished with. I have a minimum of five displayed.

Sub ShowColumns()

Columns("N:AQ").Select
Selection.EntireColumn.Hidden = True
mynum = Application.InputBox("How many provinces to display (min 5 / max 35)?")

If mynum < 5 Or mynum > 35 Then

Msgbox "Please select a number between 5 and 35", vbOKOnly, "Incorrect Entry"

Else

Range("M1").Select
numRows = Selection.Rows.Count
numColumns = Selection.Columns.Count
Selection.Resize(numRows + 0, numColumns + mynum - 5).Select
Selection.EntireColumn.Hidden = False

End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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