vba中Range对象的Replace方法

replace是Range对象的一个方法,用于单元格替换.

 

 

 

 

 

 

Sub replaceTest()
    Application.ReplaceFormat.Interior.Color = vbGreen
    '指定lookat参数为Whole,从而避免将21等包含2的数字也替换掉'
    Range("b2:e4").Replace what:=2, replacement:=3, lookat:=xlWhole, ReplaceFormat:=True
    
End Sub

 

 

 

 

 

 

 

 

 

 

 

 

Sub replaceTest()
    Application.ReplaceFormat.Interior.Color = vbGreen
    '指定lookat参数为Whole,从而避免将21等包含2的数字也替换掉'
    Range("b2:e4").Replace what:=2, replacement:=3, lookat:=xlWhole, ReplaceFormat:=True
    
End Sub


Sub FindLastRow()
   Dim r As Range
   'Set r = Range("b2").End(xlDown)'
   Set r = Cells(Rows.Count, 2).End(xlUp)
   MsgBox r.Row
End Sub

Sub findTableLastNum()
  Dim r As Range, maxRow As Long, i As Long
  '循环扫描第2列到5列'
  For i = 2 To 5
  '获取第i列最后一个数据的行号'
    Set r = Cells(Rows.Count, i).End(xlUp)
    '如果该行号大于之前找到的最大行号,则更新最大行号'
    If r.Row > maxRow Then maxRow = r.Row
  Next i
  
  MsgBox "最后一个数据在第" & maxRow & "行"
    
End Sub

  

Sub lastRow()
   Dim i As Long
   i = 3
   Do While Cells(i, 2) <> "" And i < Rows.Count
      i = i + 1
   Loop
   
   If Cells(Rows.Count, 2) = "" Then i = i - 1
   
   MsgBox "最后一行是" & i
      
End Sub


Sub lastRowTwo()
   Dim i As Long, r As Range
   Set r = ActiveSheet.UsedRange
   
   i = r.Row + r.Rows.Count - 1
   MsgBox "最后一行是" & i

End Sub

  

 

 

 

 

 

 

 

 

 

 

 

 

'找到一个表格的最后一个单元格'
Sub useSpecialCell()
   
   Dim r As Range
   Set r = Cells.SpecialCells(xlCellTypeLastCell)
   MsgBox r.Row
   
End Sub

Sub useSpecialCellTwo()

   Dim r As Range
   '按行序,从后向前查找'
   'xlRows'
   Set r = Cells.Find("*", after:=Range("A1"), searchorder:=xlColumns, searchdirection:=xlPrevious)
   
   If r Is Nothing Then
      MsgBox "表格中没有数据"
    Else
      'MsgBox r.Row'
      MsgBox r.Column
      
    End If
End Sub

'找到最后一个单元格,包括隐藏的,有空格的'
Sub useDo()

   Dim i As Long
   i = Rows.Count
   
   Do While i > 0
      If Cells(i, 2) <> "" Then Exit Do
      i = i - 1
   Loop
   MsgBox "最后一行是第" & i & "行"
End Sub

  

 

 

Sub demo1()
   Dim i As Long, k As Long, name As String, amount As Long
   For i = 2 To 9
      name = Cells(, 2): amount = Cells(i, 4)
      For k = 3 To 5
         If Cells(k, 6) = name And amout > Cells(k, 7) Then
            Cells(i, 1).Interior.Color = vbRed
            Exit For
         End If
     Next k
   Next i
End Sub

  

posted @ 2020-01-17 14:00  石shi  阅读(6144)  评论(0编辑  收藏  举报