Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2243|回复: 0

跟着校长学VBA之第11集:单元格选取

[复制链接]
发表于 2013-1-7 21:36 | 显示全部楼层 |阅读模式
本帖最后由 hwc2ycy 于 2013-1-7 21:44 编辑

既然是要取选单元格,那首先就得先明白如何表示单元格了。
1.表示一个单元格
  1. Sub s()
  2.     Range("a1").Select
  3.     Cells(1, 1).Select
  4.     Range("A" & 1).Select
  5.     Cells(1, "A").Select
  6.     Cells(1).Select
  7.     [a1].Select
  8. End Sub
复制代码
select方法就是选取(E文字面理解,就能猜出大概意思了)

2 表示相邻单元格区域
  1. Sub d()    '选取单元格a1:c5
  2.     MsgBox "Range(""a1:c5"")"
  3.     Range("a1:c5").Interior.ColorIndex = 3
  4.     MsgBox "Range(""A1"", ""C5"")"
  5.     Range("A1", "C5").Interior.ColorIndex = 4
  6.     MsgBox "Range(Cells(1, 1), Cells(5, 3))"
  7.     Range(Cells(1, 1), Cells(5, 3)).Interior.ColorIndex = 5
  8.     MsgBox "Range(""a1:a5"").Offset(0, 1)"
  9.     Range("a1:a5").Offset(0, 3).Interior.ColorIndex = 6
  10.     MsgBox "Range(""a1"").Resize(5, 3)"
  11.     Range("a1").Resize(5, 3).Interior.ColorIndex = 7
  12. End Sub
复制代码
选取区域.gif

3 表示不相邻的单元格区域
  1. Sub d1()
  2.     Range("a1,c1:f4,a7").Select
  3.     'Union(Range("a1"), Range("c1:f4"), Range("a7")).Select
  4. End Sub
复制代码
  1. Sub dd()    'union示例
  2.     Dim rg As Range, x As Integer
  3.     For x = 2 To 10 Step 2
  4.         If x = 2 Then Set rg = Cells(x, 1)
  5.         Set rg = Union(rg, Cells(x, 1))
  6.     Next x
  7.     rg.Select
  8. End Sub
复制代码
4.表示行
  1. Sub h()
  2.     'Rows(1).Select
  3.     'Rows("3:7").Select
  4.     'Range("1:2,4:5").Select
  5.     Range("c4:f5").EntireRow.Select
  6. End Sub
复制代码
5.表示列
  1. Sub L()
  2.     ' Columns(1).Select
  3.     ' Columns("A:B").Select
  4.     ' Range("A:B,D:E").Select
  5.     Range("c4:f5").EntireColumn.Select    '选取c4:f5所在的行
  6. End Sub
复制代码
6.重置坐标下的单元格表示方法
  1. Sub cc()
  2.     Range("b2").Range("a1") = 100
  3. End Sub
复制代码
7.表示正在选取的单元格区域
  1. Sub d2()
  2.        Selection.Value = 100
  3. End Sub
复制代码
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-9-21 02:54 , Processed in 0.246211 second(s), 5 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表