Excel VBA查找和替换代码

Excel VBA查找和替换代码,vba,excel,Vba,Excel,我有一个文本文件,当我导入Excel时如下所示: 但是,我一直在尝试编写一个VBA代码,该代码将执行以下操作: 列A是控制器。 当“97”以上的数字出现在a列时,我只想将其删除。 我只想保留第一行的“1”。 对于出现的每一个“2”,我首先要将ColB中的值复制到“2”行中,并将其粘贴到每一个“3”上,直到找到下一个“2”。 然后我想删除带有“2”的行 因此,最终文件应该如下所示: 到目前为止,我得到的是: Sub Deleterow97() 'Macro to format text fil

我有一个文本文件,当我导入Excel时如下所示:

但是,我一直在尝试编写一个VBA代码,该代码将执行以下操作:

列A是控制器。 当“97”以上的数字出现在a列时,我只想将其删除。 我只想保留第一行的“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
子格式表()
持续时间一样长
将单元格复制为字符串
Last=单元格(Rows.Count,“A”).End(xlUp).Row
对于i=2的持续时间
'从开始到结束的反向循环使更改“2”下的所有“3”更加容易'
cellToCopy=“”

如果(CDbl(Cells(i,“A”).Value)>=97和(CDbl(Cells(i,“A”).Value))如果(Cells(i,“A”).Value)>=97有什么问题?有多少行?当您在excel中打开时,您正在进行制表符删除还是固定宽度?以MS-DOS(PC-8)分隔。分隔符=逗号。列数据格式=常规。13列,而不是固定数量的行…可能是数百行或数千行。我的主要问题是当我到达替换件时。当我想用2值替换“3”时:对于出现的每一个“2”,我首先要在“2”行的ColB中复制该值,并将其粘贴到每一个“3”上,直到我点击下一个“2”。然后我想删除带有“2”的行对不起,我一直在按回车键,但是当我尝试替换函数时,我得到一个科学格式的数字来替换“3”。我甚至不确定这个科学格式的数字是“2”列B的等价物。所以在A列中,我得到:A1是“1”,这很好,但A2之后显示:“1.11E+28”……我很确定它没有正确地查看A列,找到“2”,将B列中的值复制到“2”旁边,并将其粘贴到A列中的“3”上,直到下一个“2”,然后复制下一个“2”列B值并继续……谢谢Scraappy,这肯定会有帮助,但是我的主要问题是上面提到的查找替换块。