固定宏进行过滤

时间:2019-03-21 21:53:11

标签: excel vba printing

我有这个宏来过滤工作表,但是现在假设标题仅在第1行...我如何假设标题为1-4行? (又名过滤器从第4行开始)

这基本上是为了过滤表格并将其另存为PDF文件到我们的文件中

Dim TempWks As Worksheet
Dim wks As Worksheet

Dim myRng As Range
Dim myCell As Range

'change to match your worksheet name
Set wks = Worksheets("Sheet3")

Set TempWks = Worksheets.Add

wks.AutoFilterMode = False 'remove the arrows

'assumes headers only in row 1
wks.Columns(1).AdvancedFilter Action:=xlFilterCopy, _
    CopyToRange:=TempWks.Range("A1"), Unique:=True

With TempWks
    Set myRng = .Range("a4", .Cells(.Rows.Count, "A").End(xlUp))
End With

With wks
    For Each myCell In myRng.Cells
        .UsedRange.AutoFilter Field:=1, Criteria1:=myCell.Value
Dim MyFileName As Variant
Dim MyfilePath As Variant

MyfilePath = "xxx" 'File Location

MyFileName = MyfilePath & "\" & myCell.Value & ".pdf" 'File Name

    ChDir _
    MyfilePath


wks.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
  MyFileName, Quality:= _
     xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
  OpenAfterPublish:=False
    Next myCell
End With

Application.DisplayAlerts = False
TempWks.Delete
Application.DisplayAlerts = True

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

With wks.Range(wks.Cells(4,1), wks.Cells(rows.Count, 1).End(xlUp))
    .AdvancedFilter Action:=xlFilterCopy, _
                    CopyToRange:=TempWks.Range("A1"), Unique:=True
End With

答案 1 :(得分:0)

也许原因是使用了.UsedRange。如果始终是第4行,请尝试将.UsedRange.AutoFilter ...替换为Intersect(.UsedRange,.UsedRange.Offset(4,0))。AutoFilter ...