Excel

Shades rows and/or columns in a worksheet for improved visibility

Ease of Use

Easy

Version tested with

2000 

Submitted by:

MWE

Description:

xlShadeRows and xlShadeCols shade (or color) every ?m? rows or columns to provide improved row-to-row or column-to-column visibility 

Discussion:

Trying to follow data rows or columns can be difficult and tiresome. Shading or colouring alternate rows has been a popular method to improve row-to-row visibility. xlShadeRows will color every ?so many? rows with any color (default is light green). The procedure has 5 optional arguments: 1) target worksheet {default is active sheet}; 2) first row for shading {default is 1}; 3) last row for shading {default is last row with data in any cell}; 4) row increment {default is 2}; and, 5) color to be used {default is light green}. xlShadeCols performs a similar function on columns with similar optional arguments. xlShadeRowCol is an interactive procedure that gathers shading information from the user and then calls xlShadeCols or xlShadeRows as appropriate. The example demonstrates several ways to call the shading procs with and without default values. 

Code:

instructions for use

			

Option Explicit Sub xlShadeRowCol(Optional ShadeOptions As Boolean = False) ' '**************************************************************************************** ' Function: shades rows or columns of the target worksheet ' Passed Values: ' ShadeOptions [in, boolean, OPTIONAL} flag indicating if shading options ' are to be set interactively. The user is always asked if shading is to ' be done on rows or on cols. If ShadeOptions = False, this proc calls ' xlShadeRows or xlShadeCols with normal defaults in place. ' If ShadeOptions = True, the user can change any or all optional ' arguements for xlShadeRows and xlShadeCols. '**************************************************************************************** ' ' Dim ColorNum As Long Dim First As Long Dim Last As Long Dim MsgbxTitle As String Dim Increment As Long Dim ProcOpns As String Dim RowOrCol As String Dim TempIn As String Dim xlSheetName As String Dim xlSheetRtn As Integer MsgbxTitle = "Row and Column Shading" ' ' ask user if rows or cols are to be shaded ' GetRowOrCol: RowOrCol = InputBox("row or col?", MsgbxTitle) Select Case LCase(RowOrCol) Case vbNullString, "end", "quit" Exit Sub Case "col" If ShadeOptions = False Then Call xlShadeCols Exit Sub End If Case "row" If ShadeOptions = False Then Call xlShadeRows Exit Sub End If Case Else MsgBox "invalid input", vbCritical GoTo GetRowOrCol End Select ' ' set defaults ' ColorNum = 35 First = 1 Last = 0 Increment = 2 xlSheetName = ActiveSheet.Name GetOptions: ProcOpns = _ InputBox("enter optional arg # OR the word 'shade' to shade " & _ "with current values:" & vbCrLf & _ "0 exit/quit without any shading" & vbCrLf & _ "1 sheet other than activesheet" & vbCrLf & _ "2 first row or col other than 1" & vbCrLf & _ "3 last row or col other than last populated" & vbCrLf & _ "4 shading increment other than 2 (every other)" & vbCrLf & _ "5 color other than light green", MsgbxTitle, "shade") Select Case ProcOpns Case "shade" Select Case LCase(RowOrCol) Case Is = "col" Call xlShadeCols(xlSheetName, First, Last, Increment, ColorNum) Case Is = "row" Call xlShadeRows(xlSheetName, First, Last, Increment, ColorNum) End Select Case Is = "0", vbNullString, "" Exit Sub Case Is = "1" GetxlSheetName: xlSheetName = InputBox("sheet name?" & vbCrLf & _ "enter blank/cancel to go back to previous prompt", MsgbxTitle) If xlSheetName = "" Then GoTo GetOptions End If Select Case xlSheetExists(xlSheetName) Case Is = 0 MsgBox xlSheetName & " is not a valid sheet in the active workbook", _ vbCritical, MsgbxTitle GoTo GetxlSheetName Case Is = 1 Case Is = 2 MsgBox xlSheetName & " is a CHARTSHEET in the active workbook" & vbCrLf & _ "chart sheets can not be shaded", vbCritical, MsgbxTitle GoTo GetxlSheetName End Select GoTo GetOptions Case Is = "2" GetFirst: TempIn = InputBox("first row or col?", MsgbxTitle) If TempIn = "" Then GoTo GetOptions First = TempIn If First < 1 Then MsgBox "values < 1 not allowed", vbCritical GoTo GetFirst End If GoTo GetOptions Case Is = "3" GetLast: TempIn = InputBox("last row or col?", MsgbxTitle) If TempIn = "" Then GoTo GetOptions Last = TempIn If Last < 1 Then MsgBox "values < 1 not allowed", vbCritical GoTo GetLast End If GoTo GetOptions Case Is = "4" GetIncrement: TempIn = InputBox("increment?", MsgbxTitle) If TempIn = "" Then GoTo GetOptions Increment = TempIn If Increment < 1 Then MsgBox "values < 1 not allowed", vbCritical GoTo GetIncrement End If GoTo GetOptions Case Is = "5" ColorNum = InputBox("color number?", MsgbxTitle) GoTo GetOptions Case Else MsgBox "invalid choice", vbCritical GoTo GetOptions End Select End Sub Sub xlShadeRows( _ Optional xlSheetName As String, _ Optional FirstRow As Long = 1, _ Optional LastRow As Long, _ Optional Increment As Long = 2, _ Optional ColorNum As Long = 35) ' '**************************************************************************************** ' Function; shades rows ' Passed Values ' xlSheetName [in, string, OPTIONAL] target worksheet {default = active} ' FirstRow [in, Long, OPTIONAL] first row for shading {default = 1} ' LastRow [in, Long, OPTIONAL] last row for shading {default = last ' populated row} ' Increment [in, Long, OPTIONAL] row increment for shading {default = 2} ' ColorNum [in, Long, OPTIONAL] colour for shading {default = 35} '**************************************************************************************** ' ' Dim I As Long Dim Row As Long Dim xlsheet As Worksheet ' ' if sheetname not passed, default to activesheet ' If xlSheetName = vbNullString Then xlSheetName = ActiveSheet.Name ' ' set the working sheet and retain current selection data ' On Error GoTo ErrorHandling_BadSheetName Set xlsheet = Worksheets(xlSheetName) ' ' if LastRow not passed, default to last row with any populated cells ' If LastRow = 0 Then LastRow = xlLastRow(xlsheet.Name) ' ' do actual shading ' Row = FirstRow - 1 For I = FirstRow To LastRow Row = Row + 1 If Row Mod Increment = 0 Then With xlsheet.Rows(Row).Interior .ColorIndex = ColorNum .Pattern = xlSolid End With End If Next I ' ' if a sheet other than the activesheet was shaded, inform user ' If xlsheet.Name <> ActiveSheet.Name Then MsgBox "row shading of " & xlsheet.Name & " complete.", vbInformation End If Exit Sub ErrorHandling_BadSheetName: MsgBox "sheetname passed to xlShadeRows is not valid", vbCritical End Sub Sub xlShadeCols( _ Optional xlSheetName As String, _ Optional FirstCol As Long = 1, _ Optional LastCol As Long, _ Optional Increment As Long = 2, _ Optional ColorNum As Long = 35) ' '**************************************************************************************** ' Function: shades columns ' Passed Values ' xlSheetName [in, string, OPTIONAL] target worksheet {default = active} ' FirstCol [in, Long, OPTIONAL] first column for shading {default = 1} ' LastCol [in, Long, OPTIONAL] last column for shading (default = last ' populated column) ' Increment [in, Long, OPTIONAL] col increment for shading {default = 2} ' ColorNum [in, Long, OPTIONAL] colour for shading {default = 35} '**************************************************************************************** ' ' Dim Col As Long Dim I As Long Dim xlsheet As Worksheet ' ' if sheetname not passed, default to activesheet ' If xlSheetName = vbNullString Then xlSheetName = ActiveSheet.Name ' ' set the working sheet and retain current selection data ' On Error GoTo ErrorHandling_BadSheetName Set xlsheet = Worksheets(xlSheetName) ' ' if LastCol not passed, default to last col with any populated cells ' If LastCol = 0 Then LastCol = xlLastCol(xlsheet.Name) ' ' do actual shading ' Col = FirstCol - 1 For I = FirstCol To LastCol Col = Col + 1 If Col Mod Increment = 0 Then With xlsheet.Columns(Col).Interior .ColorIndex = ColorNum .Pattern = xlSolid End With End If Next I ' ' if a sheet other than the activesheet was shaded, inform user ' If xlsheet.Name <> ActiveSheet.Name Then MsgBox "column shading of " & xlsheet.Name & " complete.", vbInformation End If Exit Sub ErrorHandling_BadSheetName: MsgBox "sheetname passed to xlShadeCols is not valid", vbCritical End Sub Function xlLastRow(Optional WorksheetName As String) As Long ' '**************************************************************************************** ' Function: find the last populated row in a worksheet '**************************************************************************************** ' ' If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name End If With Worksheets(WorksheetName) xlLastRow = .Cells.Find("*", .Cells(1), xlFormulas, _ xlWhole, xlByRows, xlPrevious).Row End With End Function Function xlLastCol(Optional WorksheetName As String) As Long ' '**************************************************************************************** ' Function: find the last populated column in a worksheet '**************************************************************************************** ' ' If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name End If With Worksheets(WorksheetName) xlLastCol = .Cells.Find("*", .Cells(1), xlFormulas, _ xlWhole, xlByColumns, xlPrevious).Column End With End Function Sub xlUnShadeAll(Optional WorksheetName As String) ' '**************************************************************************************** ' Function: unshade (set fill color to xlnone) entire active worksheet '**************************************************************************************** ' ' Dim xlRange As Range Set xlRange = Selection If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name End If Worksheets(WorksheetName).Cells.Select Selection.Interior.ColorIndex = xlNone xlRange.Select End Sub Function xlSheetExists(SheetName As String, Optional WorkBookName As String) As Integer ' Function: tests if SheetName is the name of any type of sheet in ' the target workbook. Function value of proc on return: ' 0 not the name of a recognized sheet type ' 1 traditional worksheet ' 2 chart sheet ' Dim xlobj As Object ' ' test for WorkBookName, if null, use ActiveWorkBook name ' If WorkBookName = vbNullString Then WorkBookName = ActiveWorkbook.Name ' ' test for worksheet ' On Error Resume Next Set xlobj = Workbooks(WorkBookName).Worksheets(SheetName) If Err = 0 Then ' is work sheet xlSheetExists = 1 Exit Function End If ' ' test for chart sheet ' On Error Resume Next Set xlobj = Workbooks(WorkBookName).Charts(SheetName) If Err = 0 Then ' is chart sheet xlSheetExists = 2 Exit Function End If ' ' neither chart nor work sheet, set function value ' to zero ' xlSheetExists = 0 End Function

How to use:

  1. Copy the above code.
  2. Open any workbook.
  3. Press Alt + F11 to open the Visual Basic Editor (VBE).
  4. In the left side window, hi-lite the target spreadsheet [it will likely be called VBAProject(name.xls) where name is the name of the spreadsheet]
  5. Select an existing code module for the target worksheet; or from the Insert Menu, choose Insert | Module.
  6. Paste the code into the right-hand code window.
  7. Add user-defined code as appropriate
  8. Close the VBE, save the file if desired.
  9. See ?Test The Code? below
 

Test the code:

  1. Open the example
  2. The example contains 7 VBA procedures: 4 shading procedures: xlShadeRows, xlShadeCols, xlShadeRowCol and xlUnShadeAll and 3 utilities: xlLastCol, xlLastRow, and xlSheetExists
  3. The example contains 3 sheets: a main worksheet with 5 cols of data and several command buttons; a 2nd worksheet with similar data and a chartsheet.
  4. The ?xlShadeRows? button calls xlShadeRows with all arguments at their default settings.
  5. The ?xlShadeCols? button calls xlShadeCols with all arguments at their default settings.
  6. The ?xlShade? button calls xlShadeRowCol assuming the user wants the default conditions
  7. The ?xlShade (with options)" button calls xlShadeRowCol assuming the user want to set shading conditions
  8. The ?xlUnShadeAll? button calls xlUnShadeAll, a utility that ?unshades? the active worksheet (so the user may try various shading options)
  9. xlLastCol and xlLastRow are utility functions that determine the last column or row with some data in any cell and are called by both xlShadeRows and xlShadeCols. xlSheetExists is a utility that tests for sheetname validity and is used by xlShadeRowCol if the user specifies a sheet for shading other than the active sheet
 

Sample File:

xlShade.zip 36.25KB 

Approved by mdmackillop


This entry has been viewed 271 times.

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