Excel Macro read cell values - vba

I am looking to put together a program simply
It reads a line in excel A1:E1
If A1 has a value it moves to B1 if B1 has a value it moves to C1 etc.
Then it stops on the last cell with a value.
Then it saves the file based on the cell value.
I ended up with this code.
Sub NamePDF()
Dim fp As String
Dim wb As Workbook
fp = "C:\Users\mdowney\Desktop\TestFolder\" & Cells(1, Columns.Count).End(xlToLeft)
Set wb = ActiveWorkbook
wb.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fp, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub

try something like
activeworkbook.saveas "C:\temp\" & cells(1,columns.count).end(xltoleft) & ".xlsx"
This will not save the macros with the workbook though.

Related

Excel:VBA code to Loop through combo box list and generate PDF for each.

I'm trying to create VBA code that selects each name one by one in a ComboBox on my worksheet. The linked Cell to the ComboBox is "D5"(which are employee names). However the ComboBox range where the names(about 1700 names) are located is on another worksheet in my workbook named "Sheet1" in column C.
When selecting a name from the ComboBox it appears in the link cell "D5". The value in "D5" is then used by other cells on the worksheet and creates a summary for the name selected. I need a macro that will select each name one by one from the ComboBox and save as a PDF.
What is a VBA Code that I can use that will loop through each name and save as PDF, as the name selected from the ComboBox into this folder C:\Users\SM\Desktop\ScorecardPDF ?
This is my final code. For those who are experiencing the same issue.
Sub pdfProduce()
Dim rngLoopRange As Range
Dim wsSummary As Worksheet
Dim rngDealers As Worksheet
Set wsSummary = Sheets("Summary")
For Each rngLoopRange In Worksheets("owssvr").Range("$B$2:$B$1648")
wsSummary.Range("D5").Value = rngLoopRange.Value
wsSummary.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="C:\Users\S\Desktop\ScorecardPDF\" & rngLoopRange.Value & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next rngLoopRange
Set wsSummary = Nothing
End Sub

Bloomberg data does not refresh while running vba

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

Converting workbook in PDF without blank pages

Hiii
I wrote a code to export sheets from one workbook to an other workbook and then convert it in pdf, but I have a lot of blank pages (maybe because of hidden formula or I don't know.
If you have any idea for what to add to my code in order to have a decent file it would be very appreciated.
Workbooks.Open FileName:="C:\Users\User\Documents\Tests Salome\dailypdf.xlsx"
Dim wbto2 As Workbook: Set wbto2 = Workbooks("dailypdf.xlsx")
wb.Activate
For Each sht In Sheets
If sht.Name <> "USD" And sht.Name <> "Balance" Then
Else
sht.Copy Before:=wbto2.Sheets(wbto2.Sheets.Count)
Rows("140:351").EntireRow.Delete '(I tried to delete the hidden rows)
End If
Debug.Print sht.Name
Next
wbto2.Activate
Application.DisplayAlerts = False
Sheets("Sheet1").Delete
Application.DisplayAlerts = True
FileName = Create_PDF(Source:=wbto2, _
FixedFilePathName:=iFile, _
OverwriteIfFileExist:=True, _
OpenPDFAfterPublish:=False)
The code functions but the result does not satisfy me because of the blank pages..
you can try any option below
1.Delete all unwanted rows before saving as PDF.
2.Set Print area
3.try to save excel range as PDF directly
'Enter Worksheet name, range Address, PDF file path and name
Sheets("Sheet Name").Range("A1:D50").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Temp\PDF_name.pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True

Object required error. saving ActiveSheet to new workbook

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

excel VBA PDF print eparating with horizontal page breaks

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?