I am currently working on a VBA code that does the following:
1.Copies a ticker from one sheet to another (same workbook)
The sheet where the ticker is copied to should be refreshed and populated by bloomberg
That sheet is exported as a PDF
Do the same process while there are no more tickers in the first sheet.
I am having issues, because the code that I created does not seem to work properly with the Bloomberg. The iteration works well, so does the export to PDF part. However, while the loop is going, there is not enough time for Bloomberg to refresh the sheet I want to export. I end up with three sheets out of four with N/As instead of the correct data.
Here is what I have so far
Public Sub RefreshStaticLinks()
Call Worksheets("M").UsedRange.Select
Call Application.Run("RefreshCurrentSelection")
Call Application.OnTime(Now + TimeValue("00:01:00"), "M_PDF")
End Sub
Sub Mosaic_PDF()
Dim File_Path As String
Dim File_Name As String
Dim ReportTic As String
Dim Rng As Range, cl As Range
Set Rng = Worksheets("Set_up").Range("A2:A300")
With Worksheets("Mosaic")
For Each cl In Rng
If cl <> "" Then
Sheets("Mosaic").Range("G3") = cl
With Worksheets("Mosaic")
Call Application.Run("RefreshStaticLinks")
End With
File_Path = "N:\DATA\EQTY\EQFUND\SI\Projects\Standard Reporting\PDF Reports\Mosaic Fundamental Positioning\"
File_Name = "Fundamental Positioning Summary "
ReportTic = Format(ActiveWorkbook.Sheets("Mosaic").Range("G3").Text)
'Exports to pdf
With ActiveWorkbook
With Sheets(Array("Mosaic")).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=File_Path & File_Name & ReportTic & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End With
End With
End If
Next cl
End With
End Sub
Related
I am looking for some help to solve a save to PDF problem. I have a sheet with some information that prints on 2 pages. The information displayed on this sheet is controlled by a drop down option to select 1 of several hundred options. I have a print to PDF function working fine for a single view of the information displayed so I select 1 option using the drop down and click the save to PDF button which all works great.
I need to add a 2nd function that will loop through all of the options available in the drop down box and add all of this into a single PDF to be saved. I can handle all of the standard code in the loop but how do I build up the PDF file inside the loop. I know how to build an array of sheets to export to a single PDF but this kind of 'in loop' function is something I am struggling to find an answer to.
Can anyone help please? If you need any more info, please just ask.
Thanks
In the Solution, suppose that we have a table which named: Table2.
We have also a help sheet(to store filtered tables) which is Hiden and named: Help.
Option Explicit
Sub print_to_pdf()
Dim sh As Long
Dim rg As Range
Dim Rng As Range
Dim rw As Range
Application.ScreenUpdating = False
For Each rw In Range("Table2[#All]").Rows
If rw.EntireRow.Hidden = False Then
If Rng Is Nothing Then Set Rng = rw
Set Rng = Union(rw, Rng)
End If
Next
Rng.Copy
With Sheets("help")
.Visible = True
sh = .Cells(Rows.Count, "A").End(xlUp).Row + 2
Set rg = Range("a3" & ":" & "a" & sh - 2)
.Activate
.Cells(sh, "A").Select
ActiveSheet.Paste
ActiveSheet.PageSetup.PrintArea = rg
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "\rep.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
.Visible = False
End With
Application.ScreenUpdating = True
MsgBox "Your PDF Has been Created with Success!!", vbInformation
End Sub
You can append data in Help sheet then export pdf.
Ref: https://stackoverflow.com/questions/
Have worked to copy a range of cells as picture to a seperate worksheet here. The range of cells automatically populate based on self selection on another worksheet - so this effectively builds a series of images intended to be and indicator pack.
Have now written something to export to PDF - the print area uses the offset function depending on the number of indicators selected.
But the export to PDF runs but does not pick up the pictures - can you help?
Sub printPDFSave()
Dim rng As Range
Dim fPathFile As Variant
Set rng = [Destination]
fPathFile = Application.GetSaveAsFilename(filefilter:="PDF Files (*.pdf), *.pdf")
If fPathFile <> False Then
rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fPathFile, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End If
End Sub
please try the below..we suppose that images are in sheet2
Option Explicit
Sub printPDFSave()
Dim rng As Range
Dim fPathFile As Variant
Dim sh As Shape
Dim ShRow As Long
For Each sh In Sheet2.Shapes
ShRow = sh.BottomRightCell.Row
Nex
Set rng = Sheet2.Range("A1:K" & ShRow)
fPathFile = Application.GetSaveAsFilename(filefilter:="PDF Files (*.pdf), *.pdf")
If fPathFile <> False Then
rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fPathFile, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End If
End Sub
I found an old script online to close the document without saving the changes, then re-open the document:
Sub RevertFile()
wkname = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
ActiveWorkbook.Close Savechanges:=False
Workbooks.Open Filename:=wkname
End Sub
I want this since you can't "undo" changes caused by running a macro. However, it does not seem to work in MS Office v1609. Firstly, the document does not re-open after it is closed. Secondly, the modifications are saved when I want them not to be. How can I rewrite this script to get it to work? Thanks.
[edit]
Here is the other sub-routine I am using.
Sub FixPlatforms()
'PURPOSE: Find & Replace a list of text/values throughout entire workbook
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
Dim sht As Worksheet
Dim platList As Variant
Dim x As Long
platList = Array _
( _
"PS4", "PlayStation 4", _
"PS3", "PlayStation 3", _
"PS2", "PlayStation 2", _
"PSV", "PlayStation Vita", _
"PSP", "PlayStation Portable", _
"WIN", "Microsoft Windows", _
"SNES", "Super Nintendo Entertainment System" _
)
'Loop through each item in Array lists
For x = 1 To UBound(platList) Step 2
'Loop through each worksheet in ActiveWorkbook
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace What:=platList(x), Replacement:=platList(x - 1), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
Next x
End Sub
Is there something wrong with it?
You shouldn't have to close the workbook in any event. Attempting to open a workbook that is already open produces the following.
Adding application.displayalerts = false should be sufficient to avoid that confirmation.
Option Explicit
Sub RevertFile()
Dim wkname As String
wkname = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
Application.DisplayAlerts = False
Workbooks.Open Filename:=wkname
Application.DisplayAlerts = True
End Sub
The purpose of this macro is to select each item in a drop down data validation list, update the sheet and then save the sheet as a new workbook. I get the error "Object required" for the block of code after ws.SaveAs. Can anyone see the problem, as I cannot :/
Sub Create_excel_sheets()
Dim strValidationRange As String
Dim rngValidation As Range
Dim rngDepartment As Range
Dim ws As Worksheet
strValidationRange = Range("AD5").Validation.Formula1
Set rngValidation = Range(strValidationRange)
For Each rngDepartment In rngValidation.Cells
Range("AD5").Value = rngDepartment.Value
ActiveSheet.Calculate
Set ws = ActiveSheet
ws.SaveAs _
FileFormat:=52, _
Filename:="C:\Test\" & rngDepartment.Value.xlsx, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next
End Sub
It seems that you are using the parameters for a Worksheet.ExportAsFixedFormat Method inside a Worksheet.SaveAs method.
Additionally, the filename:= string concatenation is a little wonky with & rngDepartment.Value.xlsx. Best to simply leave off the file extension and allow the FileFormat:= parameter to supply the correct one. In this case, the XlFileFormat Enumeration you chose (e.g. 52) is for xlOpenXMLWorkbookMacroEnabled but you seem to be trying to append .xlsx on the end.
ws.SaveAs FileFormat:=xlOpenXMLWorkbookMacroEnabled, _
Filename:="C:\Test\" & rngDepartment.Value '<~~no extension
I'm not sure if you were trying to go with .SaveAs or .ExportAsFixedFormat. I've chosen a simple .SaveAs for demonstration
I have a worksheet that has 160 pages in it. One set of data could have 3 pages followed by a horizontal page breaks. I am trying to figure out how to make a separate PDF after each horizontal page break and name it as the string in cell A (it is the same name down) of that page break.
This is where I am at with the export to pdf, missing the above.
Sub Print_PDF()
Dim Awb As Workbook
Dim Snr As Integer
Dim ws As Worksheet
Set Awb = ActiveWorkbook
For Each ws In Awb.Sheets
If ws.Visible = xlSheetVisible Then
'Sheets(ws.Name).Copy
Awb.Sheets(ws.Name).Copy
'Sheets(ws.Name).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
Awb.Path & "\" & Awb.Sheets(ws.Name).Name & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
ActiveWindow.Close False
End If
Next ws
End Sub
Are you saying you have a workbook with 160 worksheets in it? Or a workbook with 1 worksheet in it with 160 sets of data on the one sheet?