Excel VBA 中单元格选取

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()  'EntireRow属性

      'Rows(1).Select
      'Rows("3:7").Select
      'Range("1:2,4:5").Select
       Range("c4:f5").EntireRow.Select

    End Sub

'5 表示列

   Sub L()    'EntireColumn 属性

      ' Columns(1).Select
      ' Columns("A:B").Select
      ' Range("A:B,D:E").Select
      Range("c4:f5").EntireColumn.Select '选取c4:f5所在的行

   End Sub


'6 表示正在选取的单元格区域

   Sub d2()
     Selection.Value = 100
   End Sub

例子:选取当前工做表中含有>12的数字所在的行:
方法一:定位
“定位”能够在所选区域中定位大于12的数字所在的单元格,若须要删除这些单元格所在的行,定位后右键——删除整行便可(具体步骤此文不作详述)web

方法二:以下代码:svg

Public Sub 选取整行()


   Dim myrange As Range
   Dim currentRange As Range

   Set myrange = Range("a1")
   Dim myrow As Integer, mycol As Integer

     myrow = ActiveSheet.UsedRange.Rows.Count
     mycol = ActiveSheet.UsedRange.Columns.Count


     For i = 1 To myrow
         For j = 1 To mycol
             If IsNumeric(Cells(i, j)) And Cells(i, j).Value > 12 Then

                 Set currentRange = Cells(i, j).EntireRow

                 Set myrange = Union(myrange, currentRange)

             End If
         Next j
     Next i

     myrange.Select


End Sub

选取前

选取整行