Vba 以原始格式而非科学符号显示数字

Vba 以原始格式而非科学符号显示数字,vba,excel,Vba,Excel,我有一个文本文件,当我导入excel时如下所示: 我一直在努力做到以下几点: A列是控制器。 当“97”以上的数字出现在a列时,我想将其删除。 我只想保留“1”的第一行。 对于出现的每个“2”,我首先要复制“2”行中B列中的值,并将其粘贴到每个“3”上,直到找到下一个“2”。 然后我想删除带有“2”的行 因此,最终文件应该如下所示: 到目前为止,我得到的是: Sub Deleterow97() 'Macro to format text file to readable format for

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

我一直在努力做到以下几点:

A列是控制器。
当“97”以上的数字出现在a列时,我想将其删除。
我只想保留“1”的第一行。
对于出现的每个“2”,我首先要复制“2”行中B列中的值,并将其粘贴到每个“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

当replace函数为A列中的每一个“2”复制B列中的值时,它会每3个粘贴一个数字,看起来像:1.1111111 0+28

你不需要循环太多次,因为你可以使用OR in 1循环,你也不需要这些变量来完成一次性操作,这并不能解决您的问题,但下面是一个更简短、更易于管理的代码版本:

我在不确定它应该做什么的地方放了一些评论

Sub Deleterow97()
'Macro to format text file to readable format for client
For i = Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1
    If Cells(i, "A").Value = "97" Or Cells(i, "A").Value = "98" Or Cells(i, "A").Value = "99" Or Cells(i, "A").Value = "1" Then Cells(i, "A").EntireRow.Delete
Next i
With Sheets("Sheet1")   ' Replace Sheet1 by the name of your sheet
    For RowCrnt = 1 To .Cells(Rows.Count, "B").End(xlUp).Row
        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
    Next
    For i = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1 'I am not sure why this is in the WITH, should it be .Cells??
        If Cells(i, "A").Value = "2" Then Cells(i, "A").EntireRow.Delete 'See above comment Also don't use brackets around the cells(blahblah) in the If, you don't need them
    Next i
End With
End Sub

什么不起作用?你的问题是什么?对不起Tobias,我真傻!这一切似乎都正常,但当replace函数为A列中的每一个“2”复制B列中的值时,它会每3个粘贴一个数字,看起来像:1.11111110+28对不起,这对这里的某个人来说可能是一个非常简单的修复,但我完全迷路了!:(非常感谢您的帮助!谢谢Dan,您已经做了更改,但是宏完成后,A列的格式似乎仍然是科学的。应该是什么格式?您可以在循环时设置它。它应该只是A列“2”旁边B列中内容的复制粘贴,直到您点击下一个“2”在Col A。好的,我明天会看一看,我现在不在机器旁。谢谢Dan,谢谢!:)