|
|
|
|
|
|
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
If Selection.Information(wdWithInTable) = False Then
MsgBox "Selection is not in a table. Exiting macro."
Else
lngStart = Selection.Range.Start
lngEnd = Selection.Range.End
iSelectionRowEnd = Selection.Information(wdEndOfRangeRowNumber)
iSelectionColumnEnd = Selection.Information(wdEndOfRangeColumnNumber)
Selection.Collapse Direction:=wdCollapseStart
iSelectionRowStart = Selection.Information(wdEndOfRangeRowNumber)
iSelectionColumnStart = Selection.Information(wdEndOfRangeColumnNumber)
Selection.MoveEnd Unit:=wdCharacter, Count:=lngEnd - lngStart
MsgBox "The selection covers " & Selection.Cells.Count & " cells, from Cell(" & _
iSelectionRowStart & "," & iSelectionColumnStart & ") to Cell(" & _
iSelectionRowEnd & "," & iSelectionColumnEnd & ")."
End If
End Sub
|
How to use:
|
- Unzip the attached file.
- Open the file.
|
Test the code:
|
- Place the Selection anywhere in the document.
- Press Alt-N to run the macro.
- If the Selection is not in a table, the code says so, and exits.
- 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.
- 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.
|
|