Word

Find out what cells in a table are selected

Ease of Use

Easy

Version tested with

2002 

Submitted by:

fumei

Description:

Returns the number of cells selected in a table, as well as the start and end row and column numbers. 

Discussion:

While of course you can simply LOOK at the document and see what cells are selected, there can be reasons that you may want to know more imformation. The code returns the number of cells that are selected. It also returns the starting and ending cells using standard Cell(x,y) syntax. It may be useful for making selections and storing the returned values into variables for later batch processing. The user could select a portion of a table, run the code, and have those selected cell ranges stored. Then go on to another table, run the code, and have those selected cell ranges stored. NOTE: the supplied code does NOT do this. However, it could easily be adapted to do so. The supplied code picks up the Selection range END information. It then collapses the range, picks up the range information, RESELECTS the original selection, then displays a message about the selection. NOTE: the reselection instruction itself may be useful. 

Code:

instructions for use

			

Option Explicit Sub SelectionInfo() ' Dim iSelectionRowEnd As Integer Dim iSelectionRowStart As Integer Dim iSelectionColumnEnd As Integer Dim iSelectionColumnStart As Integer Dim lngStart As Long Dim lngEnd As Long ' Check if Selection IS in a table ' if not, exit Sub after message If Selection.Information(wdWithInTable) = False Then MsgBox "Selection is not in a table. Exiting macro." Else lngStart = Selection.Range.Start lngEnd = Selection.Range.End ' get the numbers for the END of the selection range iSelectionRowEnd = Selection.Information(wdEndOfRangeRowNumber) iSelectionColumnEnd = Selection.Information(wdEndOfRangeColumnNumber) ' collapse the selection range Selection.Collapse Direction:=wdCollapseStart ' get the numbers for the END of the selection range ' now of course the START of the previous selection iSelectionRowStart = Selection.Information(wdEndOfRangeRowNumber) iSelectionColumnStart = Selection.Information(wdEndOfRangeColumnNumber) ' RESELECT the same range Selection.MoveEnd Unit:=wdCharacter, Count:=lngEnd - lngStart ' display the range of cells covered by the selection MsgBox "The selection covers " & Selection.Cells.Count & " cells, from Cell(" & _ iSelectionRowStart & "," & iSelectionColumnStart & ") to Cell(" & _ iSelectionRowEnd & "," & iSelectionColumnEnd & ")." End If End Sub

How to use:

  1. Unzip the attached file.
  2. Open the file.
 

Test the code:

  1. Place the Selection anywhere in the document.
  2. Press Alt-N to run the macro.
  3. If the Selection is not in a table, the code says so, and exits.
  4. If the Selection is in a table, the code returns a message stating how many cells are covered by the Selection, and what are the starting and ending cells.
  5. If ANY part of the Selection is outside a table, an error occurs and a message displays that the Selection is outside a table.
 

Sample File:

SelectionTable.zip 8.37KB 

Approved by mdmackillop


This entry has been viewed 157 times.

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