自动筛选以包含 Headers
Autofilter to include Headers
我有一个 excel 文件,其模板看起来像这样。
我正在尝试根据客户 ID 列过滤记录并创建 PDF 的 .我正在使用下面的 VBA 代码来完成这项工作。
Public Sub Create_PDFs()
Dim CustomerIDsDict As Object, CustomerID As Variant
Dim r As Long
Dim currentAutoFilterMode As Boolean
Set CustomerIDsDict = CreateObject("Scripting.Dictionary")
'The code looks at data on the active sheet
With ActiveSheet
'Save current UI autofilter mode
currentAutoFilterMode = .AutoFilterMode
If currentAutoFilterMode Then .AutoFilter.ShowAllData
'Create dictionary containing unique Customer IDs (column B) and associated Country (column B), keyed on Customer ID
For r = 5 To .Cells(.Rows.Count, "B").End(xlUp).Row
CustomerIDsDict(.Cells(r, "B").Value) = .Cells(r, "C").Value
Next
'For each unique Customer ID
For Each CustomerID In CustomerIDsDict.keys
'AutoFilter on column B (Field:=2) with this Customer ID
.UsedRange.AutoFilter Field:=2, Criteria1:=CustomerID
'Save filtered data as PDF file "<Customer ID> <Country>.pdf" in same folder as this workbook
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\" & CustomerID & " " & CustomerIDsDict(CustomerID) & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Next
'Restore previous autofilter, if any
If currentAutoFilterMode Then
.AutoFilter.ShowAllData
Else
.AutoFilterMode = False
End If
End With
End Sub
它根据客户 ID 正确过滤并创建 PDF's.But创建的 pdf 中缺少第 2、3、4 行。它只有第一行,然后是过滤后的值。
谁能帮我解决这个问题。
我不会像您的示例那样从第一行开始自动过滤使用的范围,而是
- a) 在单元格
A3
处定义过滤 top 单元格 显式 以获取 header 字段和
- b) 取消隐藏 header 字段后的整个隐藏行 (此处带有翻译后的标题注释)
例如像这样
'AutoFilter on column B (Field:=2) with this Customer ID
' .UsedRange.AutoFilter Field:=2, Criteria1:=CustomerID
With .Range("A3")
.AutoFilter Field:=2, Criteria1:=CustomerID
.Rows(2).EntireRow.Hidden = False
End With
'further stuff ...
我有一个 excel 文件,其模板看起来像这样。
我正在尝试根据客户 ID 列过滤记录并创建 PDF 的 .我正在使用下面的 VBA 代码来完成这项工作。
Public Sub Create_PDFs()
Dim CustomerIDsDict As Object, CustomerID As Variant
Dim r As Long
Dim currentAutoFilterMode As Boolean
Set CustomerIDsDict = CreateObject("Scripting.Dictionary")
'The code looks at data on the active sheet
With ActiveSheet
'Save current UI autofilter mode
currentAutoFilterMode = .AutoFilterMode
If currentAutoFilterMode Then .AutoFilter.ShowAllData
'Create dictionary containing unique Customer IDs (column B) and associated Country (column B), keyed on Customer ID
For r = 5 To .Cells(.Rows.Count, "B").End(xlUp).Row
CustomerIDsDict(.Cells(r, "B").Value) = .Cells(r, "C").Value
Next
'For each unique Customer ID
For Each CustomerID In CustomerIDsDict.keys
'AutoFilter on column B (Field:=2) with this Customer ID
.UsedRange.AutoFilter Field:=2, Criteria1:=CustomerID
'Save filtered data as PDF file "<Customer ID> <Country>.pdf" in same folder as this workbook
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\" & CustomerID & " " & CustomerIDsDict(CustomerID) & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Next
'Restore previous autofilter, if any
If currentAutoFilterMode Then
.AutoFilter.ShowAllData
Else
.AutoFilterMode = False
End If
End With
End Sub
它根据客户 ID 正确过滤并创建 PDF's.But创建的 pdf 中缺少第 2、3、4 行。它只有第一行,然后是过滤后的值。
谁能帮我解决这个问题。
我不会像您的示例那样从第一行开始自动过滤使用的范围,而是
- a) 在单元格
A3
处定义过滤 top 单元格 显式 以获取 header 字段和 - b) 取消隐藏 header 字段后的整个隐藏行 (此处带有翻译后的标题注释)
例如像这样
'AutoFilter on column B (Field:=2) with this Customer ID
' .UsedRange.AutoFilter Field:=2, Criteria1:=CustomerID
With .Range("A3")
.AutoFilter Field:=2, Criteria1:=CustomerID
.Rows(2).EntireRow.Hidden = False
End With
'further stuff ...