Interested in developing solutions that extend the Office experience across multiple platforms? Check out the new Office Add-ins model. Use strong passwords that combine uppercase and lowercase letters, numbers, and symbols. Weak passwords don't mix these elements. This example creates a new workbook, prompts the user for a file name, and then saves the workbook.
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Is this page helpful? Please rate your experience Yes No. The time separator separates hours, minutes, and seconds when time values are formatted. The actual character used as the time separator in formatted output is determined by your system settings.
Date separator. In some locales, other characters may be used to represent the date separator. The date separator separates the day, month, and year when date values are formatted.
The actual character used as the date separator in formatted output is determined by your system settings. Display the date as ddddd and display the time as ttttt , in that order. Display only date information if there is no fractional part to the date serial number; display only time information if there is no integer portion. Display the date as a complete date including day, month, and year , formatted according to your system's short date format setting.
Display a date serial number as a complete date including day, month, and year formatted according to the long date setting recognized by your system. The default long date format is mmmm dd, yyyy. Display the day of the week as a number 1 for Sunday through 7 for Saturday. Display the month as a number without a leading zero 1— If m immediately follows h or hh , the minute rather than the month is displayed.
Display the month as a number with a leading zero 01— Display a time as a complete time including hour, minute, and second , formatted using the time separator defined by the time format recognized by your system.
A leading zero is displayed if the leading zero option is selected and the time is before A. The default time format is h:mm:ss. Use the hour clock and display an uppercase AM with any hour before noon; display an uppercase PM with any hour between noon and P. Use the hour clock and display a lowercase AM with any hour before noon; display a lowercase PM with any hour between noon and P.
Use the hour clock and display an uppercase A with any hour before noon; display an uppercase P with any hour between noon and P. Use the hour clock and display a lowercase A with any hour before noon; display a lowercase P with any hour between noon and P. Use the hour clock and display the AM string literal as defined by your system with any hour before noon; display the PM string literal as defined by your system with any hour between noon and P.
AMPM can be either uppercase or lowercase, but the case of the string displayed matches the string as defined by your system settings. If your system is set to hour clock, the string is typical set to a zero-length string. Digit placeholder. Display a digit or a zero. If the expression has a digit in the position where the 0 appears in the format string, display it; otherwise, display a zero in that position.
If the number has fewer digits than there are zeros on either side of the decimal in the format expression, display leading or trailing zeros. If the number has more digits to the right of the decimal separator than there are zeros to the right of the decimal separator in the format expression, round the number to as many decimal places as there are zeros. If the number has more digits to the left of the decimal separator than there are zeros to the left of the decimal separator in the format expression, display the extra digits without modification.
Display a digit or nothing. If the expression has a digit in the position where the appears in the format string, display it; otherwise, display nothing in that position. This symbol works like the 0 digit placeholder, except that leading and trailing zeros aren't displayed if the number has the same or fewer digits than there are characters on either side of the decimal separator in the format expression. Decimal placeholder. In some locales , a comma is used as the decimal separator.
The decimal placeholder determines how many digits are displayed to the left and right of the decimal separator. If the format expression contains only number signs to the left of this symbol, numbers smaller than 1 begin with a decimal separator. To display a leading zero displayed with fractional numbers, use 0 as the first digit placeholder to the left of the decimal separator.
The actual character used as a decimal placeholder in the formatted output depends on the Number Format recognized by your system. Percentage placeholder. Whenever PageSetup. Zoom is set to False, the properties that determine how Excel scales the Excel worksheet are PageSetup.
At the same time, if the PageSetup. FitToPagesTall property for purposes of setting the number of pages tall to which the relevant Excel worksheet is scaled to when printing or, in the case of the example in this tutorial, converted to PDF.
This is done in…. The syntax and purpose of the PageSetup. In other words, you use the FitToPagesWide property to specify the number of pages wide the relevant Excel worksheet is scaled to when printing or converting to PDF as in this example.
As anticipated above, this End With statement simply ends the With… End With block that we've just analyzed. As I show in this example, you can make several changes or adjustments that influence the resulting PDF file by using the PageSetup object.
You can use this parameter to set the filename of the resulting PDF file. Visual Basic for Applications allows you to do either of the following :. The use of the Filename that I make in these first 3 examples may work for you in certain circumstances. After all, the material structure of those macros is enough to help you save Excel worksheets, workbooks, ranges or charts as PDF files.
However, in several situations, you'll want to use the Filename parameter in a slightly different way for purposes of creating more sophisticated macros that save Excel files as PDF. Let's take a look at some different ways you can use the Filename parameter:. If you're creating a macro to save Excel files as PDF, and those PDF files must always be saved in the same folder, you can simply hard-code the relevant file path and name using the Filename parameter of ExportAsFixedFormat.
Notice, however, how I've specified the full path of the file using the Filename argument. The consequence of this change is that the resulting PDF file is no longer saved in the default folder.
Instead, the PDF is saved in the folder that you specify with the Filename argument. In the example above, the file is saved to a OneDrive folder called Power Spreadsheets. You don't necessarily need to hard-code all of the details in the Filename parameter. You can, for example, concatenate different items for purposes of building a more flexible filename structure.
In other words, if you use this Filename structure, the new PDF file is saved in the same folder as the source Excel workbook. The reason for this is that the Workbook. Path property returns a string with the complete path to the relevant workbook.
In the case of the example above, the relevant workbook is the active Excel workbook returned by the Application. ActiveWorkbook property. Name property using the Application. ActiveSheet property to return the name of the active worksheet as the actual filename within the Filename parameter. The file path continues to be provided by the Workbook. Path property. In this particular case, the macro uses i the Application. ActiveSheet and Worksheet. Range properties to return cell F5, and ii the Range.
Value property to return the value in that cell. You can achieve similar results using slightly different structures. This includes, for example, naming the converted PDF file by concatenating several strings of text. In some cases such as when concatenating several strings of text to specify the PDF filename , it may be more appropriate to declare a variable at the beginning of the procedure to store the applicable string. Whenever you use a worksheet cell as the source for the PDF filename as in the example above , make sure that the cell contains no forbidden characters.
The VBA code samples that I provide in this section are just some of the examples of the different ways you can work with and generate the Filename argument of the ExportAsFixedFormat method. As you can see, Visual Basic for Applications provides significant flexibility in this regard. Even though the topic isn't precisely the same as that of this blog post, some of the principles and examples I provide there may be helpful to you when working with the Filename parameter of the ExportAsFixedFormat VBA method.
You can use the Application. This allows the user of your VBA application to specify the full path and filename. I cover the Application. For purposes of this macro example, it's enough to know that the GetSaveAsFilename method:. The 2 blocks of code I highlight in the image above are substantially the same as those that appear in other examples of VBA code within this Excel tutorial.
The only difference is in the Filename parameter , which is the topic we're focusing on in this section. I cover the topic of VBA variables in more detail here and here. A variable is simply a storage location that you pair with a name.
You use the variable to represent a certain value. In this example, I use the PdfFilename variable for purposes of storing the filename that the user enters into the Save As dialog box displayed by the Application. GetSaveAsFilename method in block 3 below. Even though Variant isn't the most efficient data type, it's probably the most desirable for this particular case.
The reason for this is that the GetSaveAsFilename method can return different types. For example:. This statement assigns a value to the PdfFilename variable declared in block 1 above. The value that is assigned to PdfFilename is the file name provided by the user through the Save As dialog. This Save As dialog box is displayed by the Application.
GetSaveAsFilename method. In this particular example, the GetSaveAsFilename method uses the following 3 parameters :. The following screenshot shows the Save As dialog box that is displayed by the GetSaveAsFilename method that uses the parameters above. Notice the effect of each of the arguments that I list above:. The If… Then statement within the example macro above can be broken down into the 3 following sections:. The condition tested by this particular statement is whether the variable PdfFilename is different from False.
If the condition is met PdfFilename isn't False , the statements within the If… Then block are executed. These statements are the ones that adjust the page setup as explained in macro example 3 above and export the active worksheet to a PDF file as I explain in macro examples 1 and 2 above.
If the condition isn't met PdfFilename is False , the statements within the If… Then block aren't executed. In such a case, execution of the macro ends without the Excel worksheet being converted to PDF. In such cases, you probably don't want to proceed with the process of saving the Excel file as PDF. ExportAsFixedFormat Method. I explain all of these statements in great detail within the previous macro examples here and here.
This is the line that specifies the value of the Filename parameter of the ExportAsFixedFormat method. In other words, this line is the one that specifies the filename of the PDF file that results from the conversion. In this particular case, the filename that is assigned to the PDF file is the value of the PdfFilename variable. In turn, the value of the PdfFilename is determined by the Application.
GetSaveAsFilename method in block 2 above. As a consequence of this, the filename is simply the one that is specified by the user of the application by using the Save As dialog displayed by the GetSaveAsFilename method. The last row of the If… Then statement and the sample macro in general signals the end of the If… Then statement.
As a general rule, whenever you want to save all of the worksheets within an Excel workbook, you can simply use the Workbook. ExportAsFixedFormat method. The explanations I provide throughout this Excel tutorial particularly the first section are generally applicable to the Workbook.
0コメント