如何格式化带有宏的文件名中至少包含5位数字的数字? [英] How do I format a number to have at least 5 digits in a file name with a macro?
问题描述
我正在处理一个宏,该宏涉及生成文件名并将具有该名称的excel工作表另存为pdf.
I'm working on a macro which involves generating a filename and saving an excel sheet as pdf with that name.
我能够如下生成目标文件夹名称.
I was able to generate target folder name as follows.
user_name = Environ$("UserName")
file_dir = "C:\Users\" & user_name & "\Documents\Jobs\"
文件名创建如下.
Job_No = Site & "SV" & num_from_cell
PDF = Job_No & ".pdf"
file_path = file_dir & PDF
当前,以上代码返回MWSV234.pdf
.我希望它是MWSV00234.pdf
. num_from_cell
来自Excel工作表中的一个单元格.
Currently the above code returns MWSV234.pdf
. I want it to be MWSV00234.pdf
. num_from_cell
comes from one of the cells in Excel sheet.
简而言之,我想将填充num_from_cell
留给5位数字.你能告诉我怎么做吗?
In short, I want to left pad num_from_cell
to 5 digits. Could you please tell me how?
推荐答案
使用Environ$("HomePath")
获取用户的主目录比使用Environ$("UserName")
It would be safer to use Environ$("HomePath")
to get the users home directory rather than Environ$("UserName")
file_dir = Environ$("HomePath") & "\Documents\Jobs\"
然后使用Format
格式化5个数字的前导零
Then use Format
to format with leading zeros for 5 numbers
Job_No = Site & "SV" & Format(num_from_cell, "00000")
PDF = Job_No & ".pdf"
file_path = file_dir & PDF
这篇关于如何格式化带有宏的文件名中至少包含5位数字的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!