我有一個帶有模板的 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。但創建的 pdf 中缺少第 2、3、4 行。它只有第一行,然后是過濾值。
誰能幫我這個。
uj5u.com熱心網友回復:
而不是像您的示例中那樣從第一行開始自動過濾使用的范圍,我會
- a) 在單元格中明確定義過濾頂部單元 格以獲取標題欄位和
A3 - b)取消隱藏標題欄位之后的整個隱藏行(此處帶有翻譯的標題注釋)
比如像這樣
'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 ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/411624.html
標籤:
下一篇:向表中添加多行資料
