他山之石——VBA中选取Excel单元格的方法(Range selection in Excel VBA)

又学了一招,Union方法,嘿嘿。

Option Explicit


'1 表示一个单元格(a1)
 Sub s()
   Range("a1").Select
   Cells(1, 1).Select
   Range("A" & 1).Select
   Cells(1, "A").Select
   Cells(1).Select
   [a1].Select
 End Sub


'2 表示相邻单元格区域
   
   
   Sub d() '选取单元格a1:c5
'     Range("a1:c5").Select
'     Range("A1", "C5").Select
'     Range(Cells(1, 1), Cells(5, 3)).Select
     'Range("a1:a10").Offset(0, 1).Select
      Range("a1").Resize(5, 3).Select
   End Sub
   
'3 表示不相邻的单元格区域
   
    Sub d1()
    
      Range("a1,c1:f4,a7").Select
      
      'Union(Range("a1"), Range("c1:f4"), Range("a7")).Select
      
    End Sub
    
    Sub dd() 'union示例
      Dim rg As Range, x As Integer
      For x = 2 To 10 Step 2
        If x = 2 Then Set rg = Cells(x, 1)
        
        Set rg = Union(rg, Cells(x, 1))
      Next x
      rg.Select
    End Sub
    
'4 表示行
  
    Sub h()
    
      'Rows(1).Select
      'Rows("3:7").Select
      'Range("1:2,4:5").Select
       Range("c4:f5").EntireRow.Select
       
    End Sub
    
'5 表示列
    
   Sub L()
    
      ' Columns(1).Select
      ' Columns("A:B").Select
      ' Range("A:B,D:E").Select
      Range("c4:f5").EntireColumn.Select '选取c4:f5所在的行
       
   End Sub

'6 重置坐标下的单元格表示方法

    Sub cc()
    
      Range("b2").Range("a1") = 100
      
    End Sub
    
'7 表示正在选取的单元格区域

   Sub d2()
     Selection.Value = 100
   End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值