VBA/Excel/Access/Word/Excel/Selection — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 16:33, 26 мая 2010
Содержание
- 1 Code does / does not use With-End With
- 2 Counts columns in a multiple selection
- 3 Determining the type of selected range
- 4 Find next heading
- 5 Get selection address
- 6 Get the address of selection
- 7 Is the current selection a Range
- 8 Returns a count of all cells in a selection: using the Selection and Count properties
- 9 Returns a count of the number of columns in a selection
- 10 Returns a count of the number of rows in a selection
- 11 Selection move down
- 12 To add a range name based on a selection
- 13 Toggling a Boolean property
Code does / does not use With-End With
Sub selectionDemo()
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
Selection.WrapText = True
Selection.Orientation = 0
Selection.ShrinkToFit = False
Selection.MergeCells = False
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.ShrinkToFit = False
.MergeCells = False
End With
End Sub
Counts columns in a multiple selection
Sub CountColumnsMultipleSelections()
AreaCount = Selection.Areas.Count
If AreaCount <= 1 Then
MsgBox "The selection contains " & Selection.Columns.Count & " columns."
Else
For i = 1 To AreaCount
MsgBox "Area " & i & " of the selection contains " & Selection.Areas(i).Columns.Count & " columns."
Next i
End If
End Sub
Determining the type of selected range
Sub Main()
Debug.Print AreaType(Selection)
End Sub
Function AreaType(RangeArea As range) As String
" Returns the type of a range in an area
Select Case True
Case RangeArea.Count = 1
AreaType = "Cell"
Case RangeArea.Count = Cells.Count
AreaType = "Worksheet"
Case RangeArea.Rows.Count = Cells.Rows.Count
AreaType = "Column"
Case RangeArea.Columns.Count = Cells.Columns.Count
AreaType = "Row"
Case Else
AreaType = "Block"
End Select
End Function
Find next heading
Sub FindNextHeading()
Do Until Left(Selection.Paragraphs(1).Style, 7) = "Heading"
Selection.MoveDown Unit:=wdParagraph, _
Count:=1, Extend:=wdMove
Loop
End Sub
Get selection address
Sub GetSelectionAddress()
ActiveSheet.Names.Add Name:="MyRange2", RefersTo:="=" & Selection.Address()
End Sub
Get the address of selection
Sub getSelectionAddress
Selection.Address
End Sub
Is the current selection a Range
Sub EnterAvg()
If TypeName(Selection) <> "Range" Then Exit Sub
End Sub
Returns a count of all cells in a selection: using the Selection and Count properties
Sub CountAllCells()
Dim myCount As Integer
myCount = Selection.Count
MsgBox "The total number of cell(s) in this selection is : " _
& myCount, vbInformation, "Count Cells"
End Sub
Returns a count of the number of columns in a selection
Sub CountColumns()
Dim myCount As Integer
myCount = Selection.Columns.Count
MsgBox "This selection contains " & myCount & " columns", vbInformation, "Count Columns"
End Sub
Returns a count of the number of rows in a selection
Sub CountRows()
Dim myCount As Integer
myCount = Selection.Rows.Count
MsgBox "This selection contains " & myCount & " row(s)", vbInformation, "Count Rows"
End Sub
Selection move down
Sub loopDemo()
Dim i As Integer
For i = 1 To ActiveDocument.Paragraphs.Count
Application.StatusBar = "formatting" & i & " out of " & ActiveDocument.Paragraphs.Count & "..."
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdMove
Next i
End Sub
To add a range name based on a selection
Sub AddName4()
Selection.Name = "MyRange4"
End Sub
Toggling a Boolean property
Sub ToggleWrapText()
If TypeName(Selection) = "Range" Then
Selection.WrapText = Not ActiveCell.WrapText
End If
End Sub