Hi,
I have a Work book in excel which uses macros to filter a master set of invoice data and draw 4 separate documents based on the client chosen. I then have a macro to create PDF copies of 3 of the sheets, and an excel workbook containing the invoice line details.
The Source sheet used contains around 30 named ranges, which i need to transfer to the newly created document.
Below is the code i am using:
When the new sheet is created, only 4 of the named ranges are transferred, and they all appear to refer back to the source document.
Firstly, is it due to using used range for the copy that is causing not all ranges to be transferred? although all the named ranges are in this range.
Secondly, is there a way to ensure that the named ranges in the new document refer to the relative range in the the new document, and not relate back to the source? as the source document will change everytime a new client is processed.
Any Advice you can give would be great.
Thanks,
Dan.
I have a Work book in excel which uses macros to filter a master set of invoice data and draw 4 separate documents based on the client chosen. I then have a macro to create PDF copies of 3 of the sheets, and an excel workbook containing the invoice line details.
The Source sheet used contains around 30 named ranges, which i need to transfer to the newly created document.
Below is the code i am using:
Code:
Sub ExportPDF()
Dim Data As Worksheet
Set Data = Sheets("data")
Application.ScreenUpdating = False
Dim newWB As Workbook, currentWB As Workbook
Dim newS As Worksheet, currentS As Worksheet
Dim ws1 As Worksheet, ws2 As Worksheet
Dim APheader As Long, APtotal As Long, kount As Long
Dim Formula As Worksheet
Dim Frange As Range
Dim Statement As Worksheet
Dim Remittance As Worksheet
Dim Invoice As Worksheet
Dim filen As String
Dim Fpath As String
Dim Client As Range
Dim Info As Range
Dim DPath As String
Dim salesman As Range
Set salesman = Range("salesman")
Set Client = Range("Client")
Set Formula = Sheets("formula")
Set Info = Range("info")
DPath = "\\xxx.xxx.x.xx\Accounts\Remittances\Working Folder\"
'create Directories
If Len(Dir(DPath & salesman, vbDirectory)) = 0 Then
MkDir DPath & salesman
End If
If Len(Dir(DPath & salesman & "\" & Client.Value, vbDirectory)) = 0 Then
MkDir DPath & salesman & "\" & Client.Value
End If
'Set file save path and range for file name
Fpath = DPath & salesman & "\" & Client.Value & "\"
filen = Range("invoicenumber")
Set Statement = Sheets("Statement")
Set Remittance = Sheets("Remittance Advice")
Set Invoice = Sheets("Invoice")
'Export PDF files
Statement.Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Fpath & filen & "_Statement" & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Remittance.Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Fpath & filen & "_Remittance" & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Invoice.Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Fpath & filen & "_Invoice" & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Data.Activate
'Copy the data sheet
Set currentWB = ThisWorkbook
Set currentS = currentWB.Sheets("Data")
currentS.UsedRange.Select
Selection.Copy
Set newWB = Workbooks.Add
With newWB
Set newS = newWB.Sheets("Sheet1")
newS.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
newS.Range("A1").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Rows("1:6").Select
Selection.EntireRow.Delete
Columns("A:H").Select
Selection.EntireColumn.AutoFit
.SaveAs Filename:=Fpath & filen & ".xlsx"
Application.PrintCommunication = False
With newWB.ActiveSheet.PageSetup
.Zoom = 100
.FitToPagesWide = 1
.FitToPagesTall = False
End With
.Save
Application.PrintCommunication = True
ActiveWorkbook.Close
Application.DisplayAlerts = True
End With
Formula.Activate
With Sheets("formula")
If .AutoFilterMode Then .AutoFilterMode = False
With .Range("info")
.AutoFilter
.AutoFilter Field:=1, Criteria1:=Range("Client").Value
Set Frange = Range("a2", Range("a" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
Frange.EntireRow.Interior.ColorIndex = 6
End With
End With
Data.Activate
Range("A1").Select
Application.CutCopyMode = False
Formula.AutoFilterMode = False
Application.ScreenUpdating = True
End Sub
When the new sheet is created, only 4 of the named ranges are transferred, and they all appear to refer back to the source document.
Firstly, is it due to using used range for the copy that is causing not all ranges to be transferred? although all the named ranges are in this range.
Secondly, is there a way to ensure that the named ranges in the new document refer to the relative range in the the new document, and not relate back to the source? as the source document will change everytime a new client is processed.
Any Advice you can give would be great.
Thanks,
Dan.
Last edited: