我有一个文本文件,当我导入到excel时看起来像这样:
我一直在努力做到以下几点:
A栏是控制器
如果在Col A中出现“97”以上的数字,我想删除它
我只想让“1”的第一行保留
对于出现的每个“2”,我首先想要将“B”中的值复制到“2”行中,然后将其粘贴到每个“3”上,直到我点击下一个“2”。
然后我想用“2”删除Row。
所以最终文件看起来应该是这样的:
到目前为止我得到的是:
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
当替换功能在Col A中为Col A中的每个“2”复制值时,它会在每3个上粘贴一个看起来像这样的数字:1.11111110 + 28
答案 0 :(得分:0)
你不需要循环这么多次,因为你可以在一个循环中使用OR,而你真的不需要那些变量,这不能解决你的问题,但这里要短得多并且更容易管理代码版本:
我在我不确定应该做什么的地方写了几条评论。
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