|
|
|
|
|
|
Excel
|
Position Any Cell Top Left in Window
|
|
Ease of Use
|
Easy
|
Version tested with
|
2000, 2003
|
Submitted by:
|
johnske
|
Description:
|
The cell you require is positioned in the top left hand corner of the workbook window.
|
Discussion:
|
It is often very desirable to position a certain cell in the top left hand corner of the window. A frequent example of this is having the cell A1 always showing in the top left hand corner when a workbook is opened. This goal can sometimes be achieved by using 'Select', however this is very unreliable simply because it's possible to select or activate cells that aren't in the visible portion of the window (Excel doesn't always assume they also need to be visible). You can write code that rectifies this situation, but there is already a simple inbuilt function that does this - the GoTo function, this is a one-line statement that replaces four (or more) lines of code that would normally be required to do the same thing using Select. The code below is an example that positions A1 top-left on every sheet when the workbook is opened (the zipped attachment includes some other examples).
|
Code:
|
instructions for use
|
Option Explicit
Private Sub Workbook_Open()
Dim Sheet As Worksheet
Const TopLeft As String = "A1"
Application.ScreenUpdating = False
For Each Sheet In Worksheets
Application.Goto Sheet.Range(TopLeft), scroll:=True
Next
Sheet1.Activate
End Sub
|
How to use:
|
- Open an Excel workbook
- Select Tools/Macro/Visual Basic Editor
- In the VBE window, select Tools/Project Explorer
- Select the ThisWorkbook module
- Copy and paste the code into the Module
- Now select File/Close and Return To Microsoft Excel
- Save your changes...
|
Test the code:
|
- Scroll and select any cell on all the worksheets
- Save and close the workbook
- Open the workbook and note that on every sheet A1 is now positioned in the top-left corner.
- Download the attachment and open to try some other examples
|
Sample File:
|
GoToExample.zip 13.92KB
|
Approved by mdmackillop
|
This entry has been viewed 157 times.
|
|