【发布时间】:2019-03-21 21:53:11
【问题描述】:
我有这个宏来过滤工作表,但现在它假设标题只在第 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
【问题讨论】:
-
可能是因为使用了
.UsedRange。如果总是从第 4 行开始,请尝试将.UsedRange.AutoFilter...替换为Intersect(.UsedRange, .UsedRange.Offset(4,0)).AutoFilter... -
成功了 :) 非常感谢