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