数字格式问题,因为数字跑到第二位数

时间:2017-01-18 05:37:13

标签: excel excel-vba vba

我有一个问题 数字应该以格式生成 我在这里附上链接作为参考 Condition as to cater with preset value in a format

A.S.H给出的解决方案

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

在使用A.S.H提供的解决方案之前,我正在使用此代码,它正在运行,由于需求的某些变化,我必须更改代码。

field3 = 0 & 0 & Format(Date, "YYYYMM") & CleanString(myVal) & Format(Cells(vRow, 3).Value, "00000")

是的,它确实可以解决A.S.H提供的代码,但问题是当我从上面的语句中生成数字时的输出。

当前输出错误,因为数字01在前两位数字中,01是来自myVal的通过值。
01 2017010000001

正确的输出应为
00201701的 01 00001

原始的CleanString代码与我遇到问题的部分在同一个表单模块中(代码仅供参考,以显示我在做什么)

Function CleanString(strIn As String) As String
Dim objRegex
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
 .Global = True
 .Pattern = "[^\d]+"
CleanString = .Replace(strIn, vbNullString)
End With
 End Function

myVal代码如下所示,在其他模块上生成增量编号

Public myVal As String
Sub test()
Dim fn As String, txt As String, temp
fn = ThisWorkbook.Path + "\PaymentFile01.txt"
If fn = "False" Then Exit Sub
txt = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll
With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "_(\d+)(?=\|)"
    myVal = Format$(.Execute(txt)(0).submatches(0) + 1, "_00")
    txt = .Replace(txt, myVal)
    .Pattern = "(\r\n)+$"
    Open Replace(fn, ".txt", ".txt") For Output As #1
        Print #1, txt;
        MsgBox "This is bathch No" & myVal
    Close #1
End With
End Sub

不确定哪个部分出了问题。在我看来,A.S.H给出的解决方案需要进行一些修改以满足我的要求。

先谢谢你。

1 个答案:

答案 0 :(得分:0)

最后我做到了,我找到了解决方案,简单就是这个

field8 = 0 & 0 & Left(Cells(vRow, 8).Value, 6) & CleanString(myVal) & Format(Right((Cells(vRow, 8).Value), 5), "00000")