我之前在其他按预期工作的项目中使用过的代码。我现在使用具有不同值的代码。但是,包含 " WorkGroup Manager" 以外的值的任何行都不会被删除。我现在在Excel 2013(该公司刚刚从2007年升级)工作。这会导致代码无法正常工作吗?我不知道Excel 2007和Excel 2013之间的区别是什么,因为它与VBA编码有关。
Sub Filter_WGM()
Dim PTASK_Template As Workbook
Set PTASK_Template = Workbooks("BCRS Unassigned Tasks Template.xlsm")
Dim WGMd As Worksheet
Set WGMd = PTASK_Template.Sheets("WGM")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With WGMd
Dim LRMf As Long
For LRMf = Cells(Rows.Count, 3).End(xlUp).Row To 2 Step -1
If Cells(LRMf, 3).Value <> "WorkGroup Manager" Then
Rows(LRMf).Delete
End If
Next LRMf
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
我需要使用不同的值重复此代码,以便在我将其用于此工作之后处理其他两个工作表。
答案 0 :(得分:1)
试试这个,正如我所建议的那样,我使用了with语句,并重新编写了一些代码,以便于阅读。
Sub Filter_WGM()
Dim PTASK_Template As Workbook
Dim WGMd As Worksheet
Dim LRMf As Long
Set PTASK_Template = Workbooks("BCRS Unassigned Tasks Template.xlsm")
Set WGMd = PTASK_Template.Sheets("WGM")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With WGMd
For LRMf = .Cells(.Rows.Count, 3).End(xlUp).Row To 2 Step -1
If .Cells(LRMf, 3).Value <> "WorkGroup Manager" Then
.Rows(LRMf).Delete
End If
Next LRMf
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
此外,如果您打算在各种工作表上使用它并查找各种值,您可以尝试以下方面的内容。然后,您可以启动此子并将相关参数传递给它。 (请注意,此代码未经测试)。
Sub DeleteRowsFromSheet(TargetSheet As Worksheet, ValueColumn As Integer, Value As String)
Dim MyRow As Integer
With TargetSheet
For MyRow = 2 To .Cells(.Rows.Count,ValueColumn).End(XlUp).Row
If .Cells(MyRow,ValueColumn).Value <> Value Then
.Row(MyRow).EntireRow.Delete
MyRow = MyRow - 1
End If
Next
End With
End Sub