簡體   English   中英

Excel VBA查找和替換代碼

[英]Excel VBA to find and replace code

我有一個文本文件,當我導入Excel時如下所示:

原始數據

但是,我一直試圖將VBA代碼組合在一起,以執行以下操作:

列A是控制器。 當AI列中的數字大於“ 97”時,只想刪除它。 我只想保留第一行的“ 1”。 對於出現的每個“ 2”,我首先要復制ColB中“ 2”行中的值,然后將其粘貼到每個“ 3”上,直到單擊下一個“ 2”。 然后,我想刪除帶有“ 2”的行

因此,最終文件應如下所示:

后宏

到目前為止,我得到的是:

Sub Deleterow97()
'Macro to format text file to readable format for client

    Last = Cells(Rows.Count, "A").End(xlUp).Row
    For i = Last To 1 Step -1
        If (Cells(i, "A").Value) = "97" Then
            Cells(i, "A").EntireRow.Delete
        End If
    Next i

    Last = Cells(Rows.Count, "A").End(xlUp).Row
    For i = Last To 1 Step -1
        If (Cells(i, "A").Value) = "98" Then
            Cells(i, "A").EntireRow.Delete
        End If
    Next i

    Last = Cells(Rows.Count, "A").End(xlUp).Row
    For i = Last To 1 Step -1
        If (Cells(i, "A").Value) = "99" Then
            Cells(i, "A").EntireRow.Delete
        End If
    Next i
    Last = Cells(Rows.Count, "A").End(xlUp).Row
    For i = Last To 2 Step -1
        If (Cells(i, "A").Value) = "1" Then
            Cells(i, "A").EntireRow.Delete
        End If
    Next i

Dim CellValue As String
Dim RowCrnt As Integer
Dim RowMax As Integer

With Sheets("Sheet1")   ' Replace Sheet1 by the name of your sheet
    RowMax = .Cells(Rows.Count, "B").End(xlUp).Row
    For RowCrnt = 1 To RowMax
        If .Cells(RowCrnt, 1) = "2" Then
            .Range("A:A").Replace What:="3", Replacement:=.Cells(RowCrnt, 2), LookAt:=xlPart, _
                                  SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                                  ReplaceFormat:=False
        End If
    Next

    Last = Cells(Rows.Count, "A").End(xlUp).Row
    For i = Last To 2 Step -1
        If (Cells(i, "A").Value) = "2" Then
            Cells(i, "A").EntireRow.Delete
        End If
    Next i
End With
End Sub

首先,嘗試在同一循環中執行所有操作。 您可以節省時間。 例如,您可以將前三十行替換為:

Last = Cells(Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
    If (Cint(Cells(i, "A").Value)) >= 97 Then
        Cells(i, "A").EntireRow.Delete
    End If

    If i >= 2 AND (Cells(i, "A").Value) = "1" Then
        Cells(i, "A").EntireRow.Delete
    End If

Next i

編輯:所以終於

Sub formatSheet()
Dim Last As Long
Dim cellToCopy As String

  Last = Cells(Rows.Count, "A").End(xlUp).Row
  For i = 2 To Last
    'reverse loop from start to end make it easier to change all "3" below "2"
    cellToCopy = ""
    If (CDbl(Cells(i, "A").Value)) >= 97 And (CDbl(Cells(i, "A").Value)) <= 120 Then
      ' convert to double because you deal with big numbers in column B
      ' and fixe a limit to avoid bugs
        Cells(i, "A").EntireRow.Delete
    End If

    If CDbl(Cells(i, "A").Value) = 1 Then
        Cells(i, "A").EntireRow.Delete
    End If

    If CDbl(Cells(i, "A").Value) = 2 Then
        cellToCopy = Cells(i, "B").Value
        Cells(i, "A").EntireRow.Delete
        Dim j As Long
        j = i
        Do While CDbl(Cells(j, "A").Value) = 3
            Cells(j, "A").Value = cellToCopy
            j = j + 1
        Loop
    End If
  Next i
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM