Get visible row and column number

Danilo Setton

I have a simple macro at my Excel worksheet, which does some simple calculation. What happens is that for a better user experience I want the macro to run and when it is finished to return to the cell that the user was before he activated the macro, so that the user won't see any change on the screen.

Ok, so that's not to hard:

Dim activeRow As Integer
Dim activeCol As Integer

Sub myCode

    activeRow = ActiveCell.Row
    activeCol = ActiveCell.Column

    ' code...

    Cells(activeRow, activeCol).Select

End Sub

The problem with the code above is that the line and columns that were visible before the macro runs are not the same on the end, because the macro may have scrolled the worksheet to the left, right, up or down.

For example, you can see the cell E26 on your display either if the first line visible at your worksheet is 15 or 16.

So the question here is how can I know the line and column number that are visible at the user display?

I was wondering that would be something like:

Dim topLine As Integer
Dim bottomLine As Integer

topLine = ActiveWorksheet.GetVisibleGrid.TopLine
bottomLine = ActiveWorksheet.GetVisibleGrid.BottomLine
user4039065

re: "... for a better user experience I want the macro to run and when it is finished to return to the cell that the user was before he activated the macro, so that the user won't see any change on the screen."

The easiest way to do this is never use .Select or .Activate. There are less than an isolated handful of circumstances where using .Select is preferable to direct cell and worksheet reference(s) and only half again of those are actually necessary.

See How to avoid using Select in Excel VBA macros for methods on getting away from relying on .Select and .Activate to accomplish your goals.

Get started converted your code to eliminate .Select and .Activate. If you run into problems, post your working code here or at Code Review (Excel). If it is more than a page or so, post sections of it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Specify row and column number for UICollectionView

From Dev

Caroufredsel image slider - get the number of the visible slide

From Dev

MySQL query - get row with highest number filtered by another column

From Dev

Get Column number of matching string in first row. Excel VBA

From Dev

selenium webdriver to get visible row number in table

From Dev

Is there any way to get column value on the basis of row number?

From Dev

Retrieving a list of row number from only visible cells

From Dev

Repeat row based on the number in a column

From Dev

2D array get row and column by a position number from 0 to size of the array

From Dev

Grouping by row number in a column

From Dev

JFace TableViewer get visible column width

From Dev

how to find last visible column number in vba?

From Dev

How to get row and column number of cell that matches a given value in data.frame or distance matrix?

From Dev

Get the column number per row containing specific character in R data frame

From Dev

Get array row and column number according to values in python

From Dev

How to get the row and column number based on lookup dataset in Python Pandas?

From Dev

get row and column number Google spreadsheet - App script

From Dev

MySQL QUERY to get row where column ellipse number >= 1

From Dev

Get number of visible select options

From Dev

How can I get the row and column number when the user clicks in a particular column table in jquery

From Dev

Merging Row_Number column with other column

From Dev

DevExpress winform: Get Visible Index of focused row

From Dev

selenium webdriver to get visible row number in table

From Dev

Formula to get cell values of specific column and a variable row number

From Dev

A cell contains the number of row and I want to get the value of that row on specific column

From Dev

Get a value of a row in a column

From Dev

How to get cell with row and column number by referencing to cell

From Dev

How to get sender column and row number?

From Dev

Sum only visible cells in one ROW (not column)?

Related Related

  1. 1

    Specify row and column number for UICollectionView

  2. 2

    Caroufredsel image slider - get the number of the visible slide

  3. 3

    MySQL query - get row with highest number filtered by another column

  4. 4

    Get Column number of matching string in first row. Excel VBA

  5. 5

    selenium webdriver to get visible row number in table

  6. 6

    Is there any way to get column value on the basis of row number?

  7. 7

    Retrieving a list of row number from only visible cells

  8. 8

    Repeat row based on the number in a column

  9. 9

    2D array get row and column by a position number from 0 to size of the array

  10. 10

    Grouping by row number in a column

  11. 11

    JFace TableViewer get visible column width

  12. 12

    how to find last visible column number in vba?

  13. 13

    How to get row and column number of cell that matches a given value in data.frame or distance matrix?

  14. 14

    Get the column number per row containing specific character in R data frame

  15. 15

    Get array row and column number according to values in python

  16. 16

    How to get the row and column number based on lookup dataset in Python Pandas?

  17. 17

    get row and column number Google spreadsheet - App script

  18. 18

    MySQL QUERY to get row where column ellipse number >= 1

  19. 19

    Get number of visible select options

  20. 20

    How can I get the row and column number when the user clicks in a particular column table in jquery

  21. 21

    Merging Row_Number column with other column

  22. 22

    DevExpress winform: Get Visible Index of focused row

  23. 23

    selenium webdriver to get visible row number in table

  24. 24

    Formula to get cell values of specific column and a variable row number

  25. 25

    A cell contains the number of row and I want to get the value of that row on specific column

  26. 26

    Get a value of a row in a column

  27. 27

    How to get cell with row and column number by referencing to cell

  28. 28

    How to get sender column and row number?

  29. 29

    Sum only visible cells in one ROW (not column)?

HotTag

Archive