第1章 Range(单元格)对象
范例1 单元格的引用方法
1-1 使用Range属性引用单元格区域
Sub MyRng()
Range("A1:B4, D5:E8").Select
Range("A1").Formula = "=Rand()"
Range("A1:B4 B2:C6").Value = 10
Range("A1", "B4").Font.Italic = True
End Sub
1-2 使用Cells属性引用单元格区域
Sub MyCell()
Dim i As Byte
For i = 1 To 10
Sheets("Sheet1").Cells(i, 1).Value = i
Next
End Sub
1-3 使用快捷记号实现快速输入
Sub FastMark()
[A1] = "Excel 2007"
End Sub
1-4 使用Offset属性返回单元格区域
Sub RngOffset()
Sheets("Sheet1").Range("A1:B2").Offset(2, 2).Select
End Sub
1-5 使用Resize属性返回调整后的单元格区域
Sub RngResize()
Sheets("Sheet1").Range("A1").Resize(4, 4).Select
End Sub
范例2 选定单元格区域的方法
2-1 使用Select方法
Sub RngSelect()
Sheets("Sheet2").Activate
Sheets("Sheet2").Range("A1:B10").Select
End Sub
2-2 使用Activate方法
Sub RngActivate()
Sheets("Sheet2").Activate
Sheets("Sheet2").Range("A1:B10").Activate
End Sub
2-3 使用Goto方法
Sub RngGoto()
Application.Goto Reference:=Sheets("Sheet2").Range("A1:B10"), Scroll:=True
End Sub
范例3 获得指定行的最后一个非空单元格
Sub LastCell()
Dim rng As Range
Set rng = Cells(Rows.Count, 1).End(xlUp)
MsgBox "A列的最后一个非空单元格是" & rng.Address(0, 0) _
& ",行号" & rng.Row & ",数值" & rng.Value
Set rng = Nothing
End Sub
范例4 使用SpecialCells方法定位单元格
Sub SpecialAddress()
Dim rng As Range
Set rng = Sheet1.UsedRange.SpecialCells(xlCellTypeFormulas)
rng.Select
MsgBox "工作表中有公式的单元格为: " & rng.Address
Set rng = Nothing
End Sub
范例5 查特定内容的单元格
5-1 使用Find方法查特定信息
Sub FindCell()
Dim StrFind As String
Dim rng As Range
StrFind = InputBox("请输入要查的值:")
If Len(Trim(StrFind)) > 0 Then
resize函数vba With Sheet1.Range("A:A")
Set rng = .Find(What:=StrFind, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rng Is Nothing Then
Application.Goto rng, True
Else
MsgBox "没有到匹配单元格!"
End If
End With
End If
Set rng = Nothing
End Sub
Sub FindNextCell()
Dim StrFind As String
Dim rng As Range
Dim FindAddress As String
StrFind = InputBox("请输入要查的值:")
If Len(Trim(StrFind)) > 0 Then
With Sheet1.Range("A:A")
.Interior.ColorIndex = 0
Set rng = .Find(What:=StrFind, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rng Is Nothing Then
FindAddress = rng.Address
Do
rng.Interior.ColorIndex = 6
Set rng = .FindNext(rng)
Loop While Not rng Is Nothing _
And rng.Address <> FindAddress
End If
End With
End If
Set rng = Nothing
End Sub
5-2 使用Like运算符进行模式匹配查
Sub RngLike()
Dim rng As Range
Dim r As Integer
r = 1
Sheet1.Range("A:A").ClearContents
For Each rng In Sheet2.Range("A1:A40")
If rng.Text Like "*a*" Then
Cells(r, 1) = rng.Text
r = r + 1
End If
Next
Set rng = Nothing
End Sub
范例6 替换单元格内字符串
Sub Replacement()
Range("A:A").Replace _
What:="市", Replacement:="区", _
LookAt:=xlPart, SearchOrder:=xlByRows, _
MatchCase:=True
End Sub
范例7 单元格的复制
7-1 复制单元格区域
Sub RangeCopy()
Sheet1.Range("A1:G7").Copy Sheet2.Range("A1")
End Sub
Sub Copyalltheforms()
Dim i As Integer
Sheet1.Range("A1:G7").Copy
With Sheet3.Range("A1")
.PasteSpecial xlPasteAll
.PasteSpecial xlPasteColumnWidths
End With
Application.CutCopyMode = False
For i = 1 To 7
Sheet3.Rows(i).RowHeight = Sheet1.Rows(i).RowHeight
Next
End Sub
7-2 仅复制数值到另一区域
Sub CopyValue()
Sheet1.Range("A1:G7").Copy
Sheet2.Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
Sub GetValueResize()
With Sheet1.Range("A1").CurrentRegion
Sheet3.Range("A1").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
End Sub
范例8 禁用单元格拖放功能
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论