仅当从特定工作表运行宏时,“自动过滤删除”才有效

时间:2018-07-31 17:52:22

标签: excel vba excel-vba

我有这个宏,它实际上使用了两个工作表-sheet2更新了sheet1,然后杀死了第二个工作表。

我注意到,当涉及到宏的一部分(在工作表1的A列中删除具有“删除”的行)时,如果我从工作表2运行宏,则似乎无法正常工作。来自工作表1的作品没有问题。

这是完整的代码,以防万一您需要看一下-接下来,我将重点介绍我遇到麻烦的部分。

 Public Sub Cable_Load_full()
'~~> Copy New Accounts from worksheet2
Dim ws1 As Worksheet, ws2 As Worksheet
Dim bottomL As Integer
Dim x As Integer
Dim c As Range
Dim i As Long, J As Long, LastCol As Long
Dim ws1LR As Long, ws2LR As Long
Dim ws1Rng As Range, aCell As Range
Dim SearchString

Set ws1 = Sheets("CableSocials")
Set ws2 = Sheets("CableRevised")
bottomL = ws2.Range("A" & Rows.Count).End(xlUp).Row: x = 1
x = ws1.Range("A" & Rows.Count).End(xlUp).Row
x = x + 1

For Each c In ws2.Range("A1:A" & bottomL)
 If c.Value = "New" Then
    c.EntireRow.Copy ws1.Range("A" & x)
    x = x + 1
End If
Next c

'~~> Assuming that ID is in Col B
'~~> Get last row in Col B in Sheet1
ws1LR = ws1.Range("B" & Rows.Count).End(xlUp).Row
'~~> Set the Search Range
Set ws1Rng = ws1.Range("B1:B" & ws1LR)
'~~> Adding Revise Column to worksheet 1
ws1.Columns("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Value = "Revise"

Set ws2 = Sheets("CableRevised")
'~~> Turn off Filter
ws2.AutoFilterMode = False

'~~> Get last row in Col A in Sheet2
ws2LR = ws2.Range("B" & Rows.Count).End(xlUp).Row

'~~> Loop through the range in Sheet 2 to match it with the range in Sheet1
For i = 1 To ws2LR
    SearchString = ws2.Range("B" & i).Value

    '~~> Search for the ID
    Set aCell = ws1Rng.Find(What:=SearchString, LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)


    '~~> If found
    If Not aCell Is Nothing Then
        '~~> Append values
            ws1.Cells(aCell.Row, 1).Value = ws2.Cells(i, 1).Value
            ws1.Cells(aCell.Row, 3).Value = ws2.Cells(i, 2).Value
            ws1.Cells(aCell.Row, 19).Value = ws2.Cells(i, 18).Value
            ws1.Cells(aCell.Row, 20).Value = ws2.Cells(i, 19).Value
End If
Next i
    '~~> Delete the accounts that need to be deleted
     ws1.AutoFilterMode = False
        With Range("A1", Range("A" & Rows.Count).End(xlUp))
            .AutoFilter 1, "Delete"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.Delete
 End With
     ws1.AutoFilterMode = False

     '~~> Removing New from Column B

    ws1.Columns("B").Replace What:="New", _
                         Replacement:="", _
                         LookAt:=xlPart, _
                         SearchOrder:=xlByRows, _
                         MatchCase:=False, _
                         SearchFormat:=False, _
                         ReplaceFormat:=False

    ws1.Columns("A").EntireColumn.Delete

    Call SheetKiller

End Sub

Sub SheetKiller()
  Dim s As Worksheet, t As String
Dim i As Long, K As Long
K = Sheets.Count

For i = K To 1 Step -1
    t = Sheets(i).Name
    If t = "CableRevised" Then
        Application.DisplayAlerts = False
            Sheets(i).Delete
        Application.DisplayAlerts = True
    End If
Next i
End Sub

因此,仅当我从Sheet1运行Macro时才起作用的部分是:

  '~~> Delete the accounts that need to be deleted
     ws1.AutoFilterMode = False
        With Range("A1", Range("A" & Rows.Count).End(xlUp))
            .AutoFilter 1, "Delete"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.Delete
End With
    ws1.AutoFilterMode = False

我不确定为什么-好像只从ActiveSheet中删除行一样(我想这是我从中运行宏的工作表吗?)?即使我从Sheet2运行宏,也可以使其工作吗?

感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分:1)

您需要显式引用ws1上的范围。按照编写的方式,您的代码正在活动工作表上寻找范围。

尝试一下:

    '~~> Delete the accounts that need to be deleted
    ws1.AutoFilterMode = False
        With ws1.Range("A1", ws1.Range("A" & ws1.Rows.Count).End(xlUp))
            .AutoFilter 1, "Delete"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.Delete
End With
    ws1.AutoFilterMode = False
相关问题