VBA剪切插入功能工作不正常
我试图将B列中包含“CL”的行剪切到范围之外,然后将剪切插入另一个工作表中。它做得很漂亮,但如果B列不包含“CL”,它将在电子表格中插入一个空行,而不是什么都不做。我不确定为什么要插入空行?这是密码VBA剪切插入功能工作不正常,vba,excel,range,Vba,Excel,Range,我试图将B列中包含“CL”的行剪切到范围之外,然后将剪切插入另一个工作表中。它做得很漂亮,但如果B列不包含“CL”,它将在电子表格中插入一个空行,而不是什么都不做。我不确定为什么要插入空行?这是密码 With Sheets("Data") .Select ViewMode = ActiveWindow.View ActiveWindow.View = xlNormalView .DisplayPageBreaks = False Firstrow = .U
With Sheets("Data")
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
For Lrow = Lastrow To Firstrow Step -1
With .Cells(Lrow, "B")
If Not IsError(.Value) Then
If .Value = "CL" Then .EntireRow.Cut
Sheets("Sheet1").Select
Rows("10:10").Select
Selection.Insert Shift:=xlDown
End If
End With
Next Lrow
End With
End Sub
只有当你点击了一个CL时,你才在做
EntireRow.Cut,但是你总是在做插入(即使你没有找到CL)
您的缩进使它乍一看就像您在有条件地执行剪切、选择和插入操作,但实际上您使用的是单行if表单。在这种形式下,只有之后
到行尾的部分是有条件的;后续行不是有条件的
如果我纠正你的缩进,这就是你所拥有的:
With .Cells(Lrow, "B")
If Not IsError(.Value) Then
If .Value = "CL" Then .EntireRow.Cut '<--- this is a single-line if
Sheets("Sheet1").Select '<--- this, and the next two lines, will always run if .Value is not an error value
Rows("10:10").Select
Selection.Insert Shift:=xlDown
End If
End With
只有当你点击了一个CL时,你才在做EntireRow.Cut,但是你总是在做插入(即使你没有找到CL)
您的缩进使它乍一看就像您在有条件地执行剪切、选择和插入操作,但实际上您使用的是单行if表单。在这种形式下,只有之后
到行尾的部分是有条件的;后续行不是有条件的
如果我纠正你的缩进,这就是你所拥有的:
With .Cells(Lrow, "B")
If Not IsError(.Value) Then
If .Value = "CL" Then .EntireRow.Cut '<--- this is a single-line if
Sheets("Sheet1").Select '<--- this, and the next two lines, will always run if .Value is not an error value
Rows("10:10").Select
Selection.Insert Shift:=xlDown
End If
End With