Split, Hide, Unhide Workbook example

Split, Hide, Unhide Workbook example

Preface

In excel you can toggle the working area into various views to claim correct fit for your job. Consider you have one sheet having three or four different tables which are not fit on the screen. And you need to see values from another table which is grown down to publish your contents in current table hence you left with no choice than move down your pointer or scroll down every time to cross check the values. Excel offers great features by which you can customize your worksheet view to make more user friendly like Split.

Split

It split your worksheet screen based on selection, excel auto determines the split. Each part of the split screen behaves like a separate sheet. The behavior of split can be controlled by putting little code behind which we will see later in this article.

  1. Decide the area which you want to split
  2. Navigate View ribbon
  3. Click Split button located under Window group

Data source

figure 1.0

Output

figure 1.1

Notice the gray rulers which can be adjusted by pointing the mouse pointer and drag left/right or up/down direction. Put control under any of given view and move selection left to right or up down you will see each section is behaving as a separate worksheet which helps to work side by side.

Hide

Hides current window and shows immediate backward window as face

UnHide

It will bring a dialog asking which workbook window you would like to unhide. Make a selection and say OK the selected window will be visible. Lets put some code to see how these three features can be automated using VBA, VSTO (Visual Studio Tools for Office). Following prerequisites are recommended for VSTO solution:

C# Split code example

private void btnSplit_Click(object sender, RibbonControlEventArgs e)
{
	//get active window reference
	excel.Window oWindow = Globals.ThisAddIn.Application.ActiveWindow;

	//retain selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;

	//get column number
	int colNumber = oRange.Column;

	//get row number
	int rowNumber = oRange.Row;
	
	//split
	oWindow.SplitColumn = colNumber;
	oWindow.SplitRow = rowNumber;
}

C# Hide code example

private void btnHide_Click(object sender, RibbonControlEventArgs e)
{
	//get active window reference
	excel.Window oWindow = Globals.ThisAddIn.Application.ActiveWindow;

	//toggle window
	 oWindow.Visible = false;
}

C# Unhide code example

private void btnUnHide_Click(object sender, RibbonControlEventArgs e)
{
	//get all windows reference
	excel.Windows oWindows = Globals.ThisAddIn.Application.Windows;

	//Unhide window
	oWindows["myworkbookname"].Visible = true;
}

VB.Net Split code example

Private Sub btnSplitWindowEx_Click(sender As Object, e As RibbonControlEventArgs) Handles btnSplitWindowEx.Click
	'get active window reference
	Dim oWindow As excel.Window
	oWindow = Globals.ThisAddIn.Application.ActiveWindow

	'retain selection
	Dim oRange As excel.Range
	oRange = Globals.ThisAddIn.Application.Selection

	'get column number
	dim colNumber as Integer
	colNumber = oRange.Column

	'get row number
	Dim rowNumber As Integer
	rowNumber = oRange.Row

	'split
	oWindow.SplitColumn = colNumber
	oWindow.SplitRow = rowNumber
End Sub

VB.Net Hide code example

Private Sub btnHideEodeEx_Click(sender As Object, e As RibbonControlEventArgs) Handles btnHideEodeEx.Click
	'get active window reference
	Dim oWindow As excel.Window
	oWindow = Globals.ThisAddIn.Application.ActiveWindow

	'Hide window
	oWindow.Visible=False
End Sub

VB.Net Unhide code example

Private Sub btnUnhideCodeEx_Click(sender As Object, e As RibbonControlEventArgs) Handles btnUnhideCodeEx.Click
	'get all windows reference
	Dim oWindows As excel.Windows
	oWindows = Globals.ThisAddIn.Application.Windows

	'Unhide window
	oWindows("myworkbookname").Visible = true
End Sub

VBA Split code example

Public Sub SplitVBAExample()
    'get active window reference
    Dim oWindow As Window
    Set oWindow = ActiveWindow

    'retain selection
    Dim oRange As Range
    Set oRange = Selection

    'get column number
    Dim colNumber As Integer
    colNumber = oRange.Column

    'get row number
    Dim rowNumber As Integer
    rowNumber = oRange.Row

    'split
    oWindow.SplitColumn = colNumber
    oWindow.SplitRow = rowNumber
End Sub

VBA Hide code example

Public Sub HideVBAExample()
    'get active window reference
    Dim oWindow As Excel.Window
    Set oWindow = ActiveWindow

    'Hide window
    oWindow.Visible = False
End Sub

VBA Unhide code example

Public Sub UnhideVBAExample()
    'get all windows reference
    Dim oWindows As Windows
    Set oWindows = Application.Windows

    'Unhide window
    oWindows("myworkbookname").Visible = True
End Sub

Next >> Excel PrintOut object a complete reference

Leave a Reply

Your email address will not be published. Required fields are marked *