【发布时间】:2018-08-14 06:32:24
【问题描述】:
我在这个网站上找到了一个宏,如果存在特定值,则删除行: https://www.rondebruin.nl/win/s4/win001.htm 我正在尝试修改此代码,以便不仅可以手动输入:
• 我要修改的列(例如 A)
• 还有我要删除的字符串。
这就是我在代码中手动添加以下数据的原因:
Dim Columnname As String
Dim DeleteStr As String
Columnname = Application.InputBox("Select Column", xTitleId, Type:=2)
DeleteStr = Application.InputBox("Delete Text", xTitleId, Type:=2)
With .Cells(Lrow, " & Columnname & ")
If .Value = " & DeleteStr & " Then .EntireRow.Delete
我在运行代码时遇到的问题:我遇到了一个出现“运行时错误 13”类型不匹配的窗口……确实,似乎在线上有不匹配错误: 使用 .Cells(Lrow, " & Columnname & ")
不幸的是,我无法确定错误的来源。如果有人可以帮助我,那就太好了。
提前非常感谢您。 哈维
请在下面找到我的代码:
Sub Loop_Example()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
Dim Columnname As String
Dim DeleteStr As String
Columnname = Application.InputBox("Select Column", xTitleId, Type:=2)
DeleteStr = Application.InputBox("Delete Text", xTitleId, Type:=2)
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
'We use the ActiveSheet but you can replace this with
'Sheets("MySheet")if you want
With ActiveSheet
'We select the sheet so we can change the window view
.Select
'If you are in Page Break Preview Or Page Layout view go
'back to normal view, we do this for speed
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
'Turn off Page Breaks, we do this for speed
.DisplayPageBreaks = False
'Set the first and last row to loop through
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
'We loop from Lastrow to Firstrow (bottom to top)
For Lrow = Lastrow To Firstrow Step -1
'We check the values in the selected column in this example
With .Cells(Lrow, " & Columnname & ")
If Not IsError(.Value) Then
If .Value = " & DeleteStr & " Then .EntireRow.Delete
'This will delete each row with the Value "DeleteStr"
'in the seleted Column, case sensitive.
End If
End With
Next Lrow
End With
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub
【问题讨论】: