简体   繁体   中英

Excel VBA to find and replace code

I have a text file, that when I import into Excel looks like this:

原始数据

However, I've been trying to put together a VBA code that will do the following:

Column A is the controller. When a number above "97" appears in column AI simply want to delete it. I only want to let the first row of "1's" remain. For every "2" that appears, I firstly want to copy the value in ColB in the "2" row, and paste it over every "3" until I hit the next "2". Then I'd like to delete the Row's with "2"

So eventually the file should look like:

后宏

What I've got so far is:

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

At first, try to do all operations in the same loop. You'll save time. For example you can replace the first thirty lines with that :

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

EDIT : so finally

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM