条件是满足格式的预设值 [英] Condition as to cater with preset value in a format

查看:115
本文介绍了条件是满足格式的预设值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前的VBA代码就像这样

My previous VBA code is like this

field9 = 00 & Format(Date, "YYYYMM") & CleanString(myVal) & _
         Format(Cells(vRow, 8).Value, "00000")

field9变量将用于将字段
中的excel数字从000000000000006(预设值)转换为02017010100006(从我在字段9中设置的)。 CleanString(myVal)我使用从其他模块传递自动生成序列号。

This field9 variable will be used to convert my excel numbers in a field from 000000000000006(preset value) to 02017010100006 (from what i've set in field 9). The CleanString(myVal) i use to pass autogenerate sequence number from other module.

目前我需要修改这个事情,以满足我的新要求是
002017010000006(预设值)到002017010100006.我需要保持预设值,因为我不能使用我的当前代码,因为它会有更多的数字。我需要帮助。

Currently i need to modify this thing in order to cater with my new requirement which is 002017010000006( preset value) to 002017010100006. I need to remain the preset value as i can't use my current code because it will have more digits. I need assistance for this.

我将进一步解释问题是什么。
第一部分是我的变量意图做的

i will further explain what the problem is. The first part is what my variable are meant to do

000000000000006(pulled this from text file) to 002017010100006(vba code processed)

002017010100006

1.在我的代码中预设00

2. 201701是YYYYMM

3. myval是我的运行序列号01-99

4.我的格式我的单元格进入00000让我们说如果这个字段有1,那么它将是
00001
5.我可以使用我当前的公式来执行此操作

"00""201701""01""00006"
1. preset 00 in my code
2. 201701 is YYYYMM
3. myval is my running sequence number 01-99
4. i format my cell into 00000 let say if this field having 1 then it will be 00001 5. i can do this using my current formula

问题现在是

002017010000006(pulled this from text file) to 002017010100006(vba code processed) 

1。这个002017010000006也是一个文本文件

2.如果我要使用我当前的公式,我会有这样的问题
,因为它将超过15位数00201701012017010013676

1. This 002017010000006 is from a text file too
2. if i am going to use my current formula, i will having this kind of issues as it will be more than 15 digits 00201701012017010013676

我需要解决方案从我的文本文件维护201701,而不是使用格式(日期,YYYYMM)从我的目前的解决方案

i need solution in maintaining "201701" from my text files instead of using Format(Date, "YYYYMM") from my current solutions

这是我需要的结果 002017010100006 只有15位数

This is the outcome i need 002017010100006 just 15 digits

p / s:我已经编辑了我的信息进一步了解,因为我还没有清除我的问题,这里也有一些错误的信息。对于不便,我很抱歉

p/s: i have edited my info for further understanding, as i have not cleared with my question and there's some wrong info in this too. i am sorry for the inconvenience

提前谢谢。

推荐答案

尝试这样:

Function Transform(initial As String) As String
    Transform = Left(initial, 8) & CleanString(myVal) & Format(Cells(vRow, 8).Value, "00000")
End Function

或者简单地将其分配给变量...

or simply assign it to a variable...

newString = Left(oldString, 8) & CleanString(myVal) & Format(Cells(vRow, 8).Value, "00000")

请注意,您的 CleanString(myVal)必须是长度为2的字符串。如果它具有任何长度,它仍然需要一些修改(即3位数值,最终数字将是16位数字)

Be aware that your CleanString(myVal) has to be a string of length 2. If it has any length, it still needs some modification (i.e. with a 3 digits value, the final number will be on 16 digits)

所以如果你想允许 CleanString 有两位以上,这样做总是保持一个15位数的长度:

So if you want to allow that CleanString have more than two digits, do this to keep always a 15 digits length:

    Dim s As String: s = CleanString(myVal)
    newString= Left(oldString, 8) & s & Format(Cells(vRow, 8).Value, String(7 - Len(s), "0"))

这篇关于条件是满足格式的预设值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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