Dzwebs.Net

撰写电脑技术杂文十余年

VBA-Range对象在Excel中的用法示例中篇

Admin | 2008-4-26 9:30:34 | 被阅次数 | 16108

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆
选取最边缘单元格(End属性)
'选取最下方的单元格
Sub SelectEndCell()
MsgBox "选取当前单元格区域内最下方的单元格"
ActiveCell.End(xlDown).Select
End Sub
示例说明:可以改变参数xlDown以选取最左边、最右边、最上方的单元格。

设置当前单元格的前一个单元格和后一个单元格的值
Sub SetCellValue()
MsgBox "将当前单元格中前面的单元格值设为""我前面的单元格""" & vbCrLf _
& "后面的单元格值设为""我后面的单元格"""
ActiveCell.Previous.Value = "我前面的单元格"
ActiveCell.Next.Value = "我后面的单元格"
End Sub

确认所选单元格区域中是否有公式(HasFormula属性)
Sub IfHasFormula()
If Selection.HasFormula = True Then
MsgBox "所选单元格中都有公式"
Else
MsgBox "所选单元格中,部分单元格没有公式"
End If
End Sub

 公式单元格操作

 获取与运算结果单元格有直接关系的单元格
Sub CalRelationCell()
MsgBox "选取与当前单元格的计算结果相关的单元格"
ActiveCell.DirectPrecedents.Select
End Sub

 追踪公式单元格
Sub Cal1()
MsgBox "选取计算结果单元格相关的所有单元格"
ActiveCell.Precedents.Select
End Sub
Sub TrackCell()
MsgBox "追踪运算结果单元格"
ActiveCell.ShowPrecedents
End Sub
Sub DelTrack()
MsgBox "删除追踪线"
ActiveCell.ShowPrecedents Remove:=True
End Sub

复制单元格(Copy方法)
Sub CopyRange()
MsgBox "在单元格B7中写入公式后,将B7的內容复制到C7:D7內"
Range("B7").Formula = "=Sum(B3:B6)"
Range("B7").Copy Destination:=Range("C7:D7")
End Sub

获取单元格行列值(Row属性和Column属性)
Sub RangePosition()
MsgBox "显示所选单元格区域的行列值"
MsgBox "第 " & Selection.Row & "行 " & Selection.Column & "列"
End Sub

获取单元格区域的单元格数及行列数(Rows属性、Columns属性和Count属性)
Sub GetRowColumnNum()
MsgBox "显示所选取单元格区域的单元格数、行数和列数"
MsgBox "单元格区域中的单元格数为:" & Selection.Count
MsgBox "单元格区域中的行数为:" & Selection.Rows.Count
MsgBox "单元格区域中的列数为:" & Selection.Columns.Count
End Sub

设置单元格中的文本格式

  对齐文本
Sub HorizontalAlign()
MsgBox "将所选单元格区域中的文本左右对齐方式设为居中"
Selection.HorizontalAlignment = xlHAlignCenter
End Sub
Sub VerticalAlign()
MsgBox "将所选单元格区域中的文本上下对齐方式设为居中"
Selection.RowHeight = 36
Selection.VerticalAlignment = xlVAlignCenter
End Sub

 缩排文本(InsertIndent方法)
Sub Indent()
MsgBox "将所选单元格区域中的文本缩排值加1"
Selection.InsertIndent 1
MsgBox "将缩排值恢复"
Selection.InsertIndent -1
End Sub

 设置文本方向(Orientation属性)
Sub ChangeOrientation()
MsgBox "将所选单元格中的文本顺时针旋转45度"
Selection.Orientation = 45
MsgBox "将文本由横向改为纵向"
Selection.Orientation = xlVertical
MsgBox "将文本方向恢复原值"
Selection.Orientation = xlHorizontal
End Sub

 自动换行(WrapText属性)
Sub ChangeRow()
Dim i
MsgBox "将所选单元格设置为自动换行"
i = Selection.WrapText
Selection.WrapText = True
MsgBox "恢复原状"
Selection.WrapText = i
End Sub

 将比单元格列宽长的文本缩小到能容纳列宽大小(ShrinkToFit属性)
Sub AutoFit()
Dim i
MsgBox "将长于列宽的文本缩到与列宽相同"
i = Selection.ShrinkToFit
Selection.ShrinkToFit = True
MsgBox "恢复原状"
Selection.ShrinkToFit = i
End Sub

设置条件格式(FormatConditions属性)
Sub FormatConditions()
MsgBox "在所选单元格区域中将单元格值小于10的单元格中的文本变为红色"
Selection.FormatConditions.Add Type:=xlCellValue, _
Operator:=xlLessEqual, Formula1:="10"
Selection.FormatConditions(1).Font.ColorIndex = 3
MsgBox "恢复原状"
Selection.FormatConditions(1).Font.ColorIndex = xlAutomatic
End Sub

插入批注(AddComment方法)
Sub EnterComment()
MsgBox "在当前单元格中输入批注"
ActiveCell.AddComment ("Hello")
ActiveCell.Comment.Visible = True
End Sub

隐藏/显示单元格批注
Sub CellComment()
MsgBox "切换当前单元格批注的显示和隐藏状态"
ActiveCell.Comment.Visible = Not (ActiveCell.Comment.Visible)
End Sub

改变所选单元格的颜色
Sub ChangeColor()
Dim iro As Integer
MsgBox "将所选单元格的颜色改为红色"
iro = Selection.Interior.ColorIndex
Selection.Interior.ColorIndex = 3
MsgBox "将所选单元格的颜色改为蓝色"
Selection.Interior.Color = RGB(0, 0, 255)
MsgBox "恢复原状"
Selection.Interior.ColorIndex = iro
End Sub

改变单元格的图案
Sub ChangePattern()
Dim p, pc, i
MsgBox "依Pattern常数值的顺序改变所选单元格的图案"
p = Selection.Interior.Pattern
pc = Selection.Interior.PatternColorIndex
For i = 9 To 16
With Selection.Interior
.Pattern = i
.PatternColor = RGB(255, 0, 0)
End With
MsgBox "常数值 " & i
Next i
MsgBox "恢复原状"
Selection.Interior.Pattern = p
Selection.Interior.PatternColorIndex = pc
End Sub

该杂文来自: Excel杂文

上一篇:VBA-Range对象在Excel中的用法示例上篇

下一篇:VBA-Range对象在Excel中的用法示例下篇

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计