Excel

Create a custom worksheet by hiding unwanted rows and columns

Ease of Use

Easy

Version tested with

2002 

Submitted by:

Glaswegian

Description:

You may need to create a custom worksheet, either as a separate routine or as part of a larger routine. The code needs to allow the user to choose the number of rows and columns that should remain visible and hides the remainder. 

Discussion:

As part of a workbook to which users input various sales details and client information, you may need to create a custom sheet for each user (textboxes may not be to everyone's liking - I know!). The sheet may be used as a general notes sheets or similar. However, some users want more space than others and some want less - they don't want to scroll along the sheet. This code creates a custom sheet based on user input - the user decides how many rows and columns they prefer. The code also includes error handling to ensure that the input is valid. 

Code:

instructions for use

			

Sub CustomSheetMaker() Dim x As Integer Dim y As Long Dim shtName x = Application.InputBox("Please select a number of columns", "Number of Columns") If x = False Then Exit Sub 'if the user clicks 'Cancel' exit the macro If x > 256 Or x < 5 Then 'set a minimum and maximum number or exit the macro MsgBox "You cannot have more than 256 columns" & vbCr _ & "or less than " & x & " columns." & vbCr & _ "Please start again.", vbCritical, "Error" Exit Sub End If y = Application.InputBox("Please select a number of rows", "Number of rows") If y = False Then Exit Sub 'if the user clicks 'Cancel' exit the macro If y > 65536 Or y < 5 Then 'set a minimum and maximum number or exit the macro MsgBox "You cannot have more than 65536 rows" & vbCr _ & "or less than " & y & " rows." & vbCr & _ "Please start again.", vbCritical, "Error" Exit Sub End If shtName = Application.InputBox("Type a name for the new sheet", "New sheet name") If shtName = False Then Exit Sub 'if the user clicks 'Cancel' exit macro If Len(shtName) < 3 Then 'set a minimum number of characters MsgBox "You did not enter a valid sheet name." & vbCr & _ "Please use 3 or more characters." & vbCr & _ "Please start again.", vbCritical, "Error" Exit Sub End If ActiveWorkbook.Sheets.Add after:=Worksheets(Worksheets.Count) 'move the new sheet to the end With ActiveSheet 'the new sheet automatically becomes the active sheet .Name = shtName 'name the new sheet .Range(Cells(y + 1, 256), Cells(65536, x + 1)).EntireColumn.Hidden = True 'select the cols outwith the desired area and hide - add 1 to ensure correct number left .Range(Cells(65536, x + 1), Cells(y + 1, 256)).EntireRow.Hidden = True 'select the rows outwith the desired area and hide - add 1 to ensure correct number left End With End Sub

How to use:

  1. Press Alt+F11 from Excel to open the VB Editor.
  2. Click on the Project(Workbook) name in the left pane.
  3. Click on ?Insert? on the Menu Bar
  4. Select ?Module? from the list. The new Module will open.
  5. Paste the code into the right pane of the Module.
  6. Press Alt+F11 to return to Excel
 

Test the code:

  1. Can be run in any workbook or from your Personal.xls file.
  2. Press Alt+F8 and select the macro name from the list or from the menu bar, click
  3. Tools > Macro > Macros and select the macro name from the list.
 

Sample File:

Hide Rows and Columns.zip 8.24KB 

Approved by mdmackillop


This entry has been viewed 281 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express