I have a worksheet that often needs to be printed in a specific order of worksheets and ranges. What I'm trying to do is
Print out specific worksheets and ranges in order to a single PDF file.
Save the PDF file as a specific date to a certain folder.
The obstacle I'm running into is getting the numerous sheets and ranges printed into one PDF file, as currently each worksheet or range prints out to its own single PDF file.
I'm assuming there's a way to put all the necessary sheets and ranges in something like an array and then do a .PrintOut to that variable, however I haven't gotten this to work.
I'm using Excel 2010 and so I just use the "Adobe PDF" Printer.
How can I print multiple ranges and worksheets to a single .pdf?
To print more than one worksheet, you can put the worksheet names in an array like this
Sub PrintArrayOfWorksheets()
Dim vaWorksheets As Variant
vaWorksheets = Array("Sheet1", "Sheet2", "Sheet3")
ThisWorkbook.Worksheets(vaWorksheets).PrintOut
End Sub
Printing to PDF has special problems. If you're using the "official" add-in for creating PDFs it will probably work as above (sorry I can't try it right now). In the past when I've worked with other printer drivers that print to PDF, I found that all (or at least most) of the PageSetup properties had to be identical on the sheets or it would print in multiple jobs. That means no shrink-to-fit and all the margins have to be the same. I can't remember all the properties that caused problems, just that those two definitely did and Orientation definitely didn't.
To limit the ranges that print, you need to set the print area on each sheet. You can use the PageSetup.PrintArea property to do it in code. But if doesn't change, it's probably just as well to do it manually one time and it will persist.
See code below. This was used to print multiple sheets in a custom order using a VBA Button. It prints to whatever printer is selected prior to clicking the button.
In essence what it does is selects the sheets that you want and rearranges them at the end of the workbook then prints them. after printing it reorders them to the original order.
Option Explicit
Private Sub Print_Sheets_InOrder_Click()
Dim ary
Dim orig() As String
Dim a As Variant, fp As String, i As Variant
ary = Array("Sheet1", "Sheet4", "Sheet3")
fp = ActiveWorkbook.Path
ReDim orig(1 To Sheets.Count)
For i = 1 To Sheets.Count
orig(i) = Sheets(i).Name
Next i
For Each a In ary
Sheets(a).Move after:=Sheets(Sheets.Count)
Next a
ThisWorkbook.Worksheets(ary).PrintOut
For Each a In orig
Sheets(a).Move after:=Sheets(Sheets.Count)
Next a
End Sub
Excel 2010 has a print to pdf feature. If each of your worksheets are set up to print the information that you want, then highlight the various worksheets you want to print to one pdf file, then go to "file" and "save-as" and pick save as type "pdf". It should print the worksheets you have highlighted as you have the print set up for each worksheet.
Here is an approach I have tried ... I print the worksheets to a PS file, then double-click on the PS file to generate a PDF. I know it is a workaround, but printing to PS is lightning quick. It requires you have Adobe Acrobat on your PC.
Dim GetPath, SavePath, ReportSuffix
GetPath = ActiveWorkbook.Path & "\"
ReportSuffix = Range("ReportName").Text & ".ps"
SavePath = GetPath & ReportSuffix
Dim PrintSheets
PrintSheets = Array("Exec1", "Intro1", "Intro2", "Intro3", "Intro4", "Intro5")
Sheets(PrintSheets).PrintOut Copies:=1, PrintTofile:=True, PrToFileName:=SavePath
Related
I'm trying to create an Excel macro that exports worksheets to PDF. I have a simple piece of code below that successfully exports the active sheet to the folder that I want. What I want to do - and can't find a solution for - is a way to give the user the option to export multiple worksheets to the same PDF. In my application the worksheets exported may have different names, may be created after the macro is written and may be a different number of sheets each time. I have tried to make arrays that use selection but this is beyond my own knowledge of macro writing, which is limited. In an ideal world, I'd like to use a pop-up selection box to choose the sheets to export, but I'll start with the basics of the code first.
Could someone please suggest a section of code that would suit my application?
Sub Export_PDF()
'File name
Dim saveName As String
saveName = Left(ActiveWorkbook.Name, InStrRev(ActiveWorkbook.Name, ".")) & "pdf"
'File path
Dim saveLocation As String
saveLocation = "C:\Users\" & Environ("username") & "\Temp Out\"
'Save Active Sheet(s) as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveLocation & saveName
End Sub
Excel stores worksheets as a collection, so you can easily access all the worksheets that have been created in the workbook.
To allow user to select which ones he wants to export, you can create a UserForm with a ListBox that will read all available worksheets in the workbook and display them. Sample code that does that below (the UserForm has only one listbox created ListBox1 and nothing else).
Sub export_wsheets()
Dim wsheet As Worksheet
Dim wsheets_col As worksheets
Dim uForm As New UserForm1
Set wsheets_col = ThisWorkbook.worksheets
For Each wsheet In wsheets_col
uForm.ListBox1.AddItem wsheet.Name
Next
uForm.Show
End Sub
From then on you can just save user's choice and loop through the workbooks again exporting the ones that were selected. You can access particular worksheet by using it's name or ID.
It's not a complete solution but I hope it sheds some more light on your problem.
I'm trying to use visual basic for applications to loop through a list of named ranges from an excel sheet as the first step in a process for altering their formulas. Its all part of an Excel plug in I work with using Visual Studo I have a basic loop, which came directly from the MS docs.
Dim n As Excel.Name
Dim names As Excel.Names = destStreetEstimates.Names
Dim test_string As String = destStreetEstimates.Name
MsgBox(test_string)
For Each n In names
MsgBox(Prompt:=n.Name)
Next n
This doesn't work, it looks like com objects are being put together into a list when I turn on the VS debugger (yay), but the com objects aren't being turned into anything useful I can loop through. (boo).
Am I missing something? I've gone through the Docs several times, nothing I've read there matches with the errors I'm getting. But I'm still pretty green.
https://learn.microsoft.com/en-us/office/vba/excel/concepts/cells-and-ranges/refer-to-named-ranges
I've looked at other guides, most of them seemed to be discussing how to programmatically generate named ranges. I have to start with the ranges I've been given, its about 70, and while I have all their names in a config file there's a good chance the sheet will have additional names I haven't been given in the future and my function has to work in that case too.
See https://www.thespreadsheetguru.com/blog/the-vba-guide-to-named-ranges
Sub NamedRange_Loop()
'PURPOSE: Named Ranges in the Active Workbook
'SOURCE: www.TheSpreadsheetGuru.com
Dim nm As Name
'Loop through each named range in workbook
For Each nm In ActiveWorkbook.Names
Debug.Print nm.Name, nm.RefersTo
Next nm
'Loop through each named range scoped to a specific worksheet
For Each nm In Worksheets("Sheet1").Names
Debug.Print nm.Name, nm.RefersTo
Next nm
End Sub
I'm still reasonably new to VBA and feel I'm punching a little above my weight, so hopefully someone can help.
I need to issue a spreadsheet to people in my company which they can fill out and send it back. This needs to be done multiple times, so I have tried to automate this as much as possible. The source data is pasted in an "input" tab - this is then pivoted by user and input into a template tab. I can select any user and run a macro which does this and exports the filled out template to a new workbook.
In this template tab, I have dependent drop-down lists, which I have done by data validation - this relies on named ranges from the "coding" tab, which is also exported. One named range shows a list of values, and the other indexes over this and matches it to the required cell, to ensure only valid combinations are shown.
My issue is that the new workbook must not contain any links to the master - it should function completely in its own right. However, something is going wrong with the data validation/named ranges. Either some named ranges are being deleted (I know which bit of code is doing that but without it you get prompted to update links) or the data validation formula links back to the original workbook and doesn't work. I cannot find another way of achieving what I need without this particular data validation set up, so I need to try and adjust my macro to cater for this.
Is it possible to simply copy the template and coding tabs, with all the data validation, to a new workbook and break all links to the original, so that there are no startup prompts and the drop-downs all work?
Sub Copy_To_New_Workbook()
Dim wb As Workbook
Dim name As String
Dim ExternalLinks As Variant
Dim x As Long
Dim strFolder As String, strTempfile As String
name = Worksheets("Control").Cells(14, 7).Value
Let FileNameIs = Range("Filepath").Value & Range("FileName").Value
Set wb = Workbooks.Add
ThisWorkbook.Worksheets("Coding").Copy Before:=wb.Sheets(1)
ActiveSheet.name = "Coding"
ThisWorkbook.Worksheets("Transactions").Copy Before:=Worksheets("Coding")
ActiveSheet.name = "Transactions"
With ActiveSheet.UsedRange
.Value = .Value
End With
Application.DisplayAlerts = False
Worksheets("Sheet1").Delete
Application.DisplayAlerts = True
ExternalLinks = wb.LinkSources(Type:=xlLinkTypeExcelLinks)
ExternalLinks = wb.LinkSources(Type:=xlLinkTypeExcelLinks)
For x = 1 To UBound(ExternalLinks)
wb.BreakLink name:=ExternalLinks(x), Type:=xlLinkTypeExcelLinks
Next x
Dim objDefinedName As Object
For Each objDefinedName In wb.Names
If InStr(objDefinedName.RefersTo, "[") > 0 Then
objDefinedName.Delete
End If
Next objDefinedName
On Error GoTo 0
wb.SaveAs Filename:=FileNameIs, FileFormat:=52
ActiveWorkbook.Close
End Sub
I was trying to copy a text file and paste the data in excel using the following code. The code works satisfactorily to the extent copying and pasting data in the destination excel sheet, but additionally, opens up another excel file and sheet with the same name as the Text file, without being prompted anywhere in the code, and pastes the data there as well. This is not desirable. I don't want to split the data to columns or perform any other actions. It is a plain and simple copy and paste task. I have searched this and various other websites for an answer but failed to get one that appropriately addresses my problem. I am unable to figure out the flaw in the code, and therefore, seek your help. Any help would be most thankfully acknowledged.
Here is my code:
Sub CopyTextFile()
Set TxtFileName = Workbooks.Open("D:\Spares\Inventory\list_of_spares.txt")
TxtFileName.Sheets(1).Range("A1").CurrentRegion.Copy
Workbooks("Macro Test.xlsm").Activate
ActiveWorkbook.Sheets(1).Range("A1").Select
ActiveSheet.Paste
End Sub
You're getting the "extra file" because you are opening the text file in Excel (with the Workbooks.Open statement) and then copying the data from it.
Instead, open the file with a filesystemobject and read the data, then write it directly into your workbook:
Sub CopyTextFile()
Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oFile : Set oFile = oFso.OpenTextFile("D:\Spares\Inventory\list_of_spares.txt", 1)
Dim sText
sText = oFile.ReadAll
oFile.Close
ThisWorkbook.Sheets(1).Range("A1").Value = sText
End Sub
See how that works for you?
I found a way to make your code work, you had to close the second workbook that was open, your code helped me thats why i am pointing out what was missing.
Sub CopyTextFile()
Set TxtFileName = Workbooks.Open("D:\Spares\Inventory\list_of_spares.txt")
TxtFileName.Sheets(1).Range("A1").CurrentRegion.Copy
Workbooks("Macro Test.xlsm").Activate
ActiveWorkbook.Sheets(1).Range("A1").Select
ActiveSheet.Paste
Workbooks(2).Close
End Sub
If you mention Workbooks(1).Close, it would close the first opened excel, here we want to close the second workbook hence Workbooks(2).Close
I found some excel vba code on this link which helped:https://www.excel-easy.com/vba/examples/close-open.html
I'm in the process of refactoring a huge workbook woth a lot of legacy parts, redundant computations, cross-dependencies etc.
Basically, I'm trying to remove unneeded sheets and implement some proper information flow within the workbook. Is there a good way to extract the dependencies between the sheets (with VBA)?
Thanks
Martin
You can use ShowPrecedents and NavigateArrow.
here is some pseudocode
for each oCell in oSht containing a formula
ocell.showprecedents
do until nomoreprecedents
i=i+1
Set oPrec = oCell.NavigateArrow(True, 1, i)
If not oPrec.Parent Is oSht Then
' off-sheet precedent
endif
loop
next ocell
I came up with a little sub to do this. It moves all the sheets into seperate workbooks and prints out the dependencies. The advantage over using showPrecedents is that it captures all links including names, embedded forms/diagramms etc.
Word of warning: Moving worksheets isn't undo-able, save your workbook before running this and close (without saving) and re-open afterwards.
Sub printDependencies()
' Changes workbook structure - save before running this
Dim wbs As VBA.Collection, wb As Workbook, ws As Worksheets
Dim i As Integer, s As String, wc As Integer
Set ws = ThisWorkbook.Worksheets
Set wbs = New VBA.Collection
wbs.Add ThisWorkbook, ThisWorkbook.FullName
For i = ws.Count To 2 Step -1
ws(i).Move
wc = Application.Workbooks.Count
wbs.Add Application.Workbooks(wc), Application.Workbooks(wc).FullName
Next
Dim wb As Workbook
For Each wb In wbs
For Each s In wb.LinkSources(xlExcelLinks)
Debug.Print wb.Worksheets(1).Name & "<-" & wbs(s).Worksheets(1).Name
Next
Next
End Sub
The code isn't very polished or user-friendly, but it works.
You can follow the steps at "Find external references that are used in cells" topic of the following link:
Find external references in a worbook
But instead of enter the "[" you should enter the name of the sheet you're trying to find its dependencies. It will display a large list of every single cell referencing the sheet, but at the end it works. Haven't find the way to group by Sheet.