将带有前导零的变量与另一个字符串连接 [英] Concatenate variable with leading zeros with another string

查看:57
本文介绍了将带有前导零的变量与另一个字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景

我有一个excel单元格,其中包含前导零的值(例如:0002).我的宏将这个值复制到一个名为 runNumber 的变量中,并将此 runNumber 与其他字符串串联在一起,并将其粘贴到另一个文件中.但是这样做的时候,我错过了 runNumber

I have an excel cell that contains value with leading zeros (Eg: 0002). My macro is copying this value to a variable called runNumber and pasting it into another file by concatenating this runNumber with some other string. But when it does that, I am missing the leading zeros of runNumber

代码

yearInYy ="19"

ciNumber ="PFTPA-"&年-"&runNumber

输出单元格显示为 PFTPA-19-2

我需要的

我需要显示输出单元格

PFTPA-19-0002

有人知道怎么做吗?

推荐答案

ciNumber = "PFTPA-" & yearInYy & "-" & Format(runNumber, "0000")

使用 Format 函数,您可以将日期或数字转换为具有特定格式的字符串.我们在这里所要做的就是要求4位数字,带有前导零.

Using the Format function, you can convert a date or number to a string with a specific format. All we do here is require 4 digits, with leading zeros.

Excel本身的等效项是

The equivalent in Excel itself is the TEXT function

这篇关于将带有前导零的变量与另一个字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆