ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: ovxyz

[求助] 关于字符转数字规范格式的问题。

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-2-24 15:44 | 显示全部楼层
写了一个使用正则的自定义函数供参考:
Function my_replace(rng As Range) As Double
    Dim reg As Object, theStr$
    '
    theStr = rng
    Set reg = CreateObject("VBScript.RegExp")
    With reg
        .Pattern = "^(?=.*\.)(?=.*,)"
        If .test(theStr) Then
            theStr = Replace(theStr, ".", ",")
            .Pattern = ",(?=[^,]+$)"
            theStr = .Replace(theStr, ".")
        End If
        .Pattern = "-$"
        If .test(theStr) Then theStr = "-" & .Replace(theStr, "")
    End With
    my_replace = theStr
    Set reg = Nothing
End Function

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-2-24 15:46 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 ovxyz 于 2023-2-24 15:48 编辑

我是公司的win10,提示这样的,我搜了一下,能搜到文件,但无法运行,用dos命令也无法运行,奇怪。
是不是也成vba调用reguar也是一样的?

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-2-24 16:29 | 显示全部楼层
gbgbxgb 发表于 2023-2-24 15:44
写了一个使用正则的自定义函数供参考:
Function my_replace(rng As Range) As Double
    Dim reg As Ob ...

感谢兄弟,运行没有问题。

TA的精华主题

TA的得分主题

发表于 2023-2-24 16:41 | 显示全部楼层
本帖最后由 LIUZHU 于 2023-2-24 16:44 编辑

既然是WPS,那就给一个WPS的解决方案好了,JSA写的,用JS宏运行 image.jpg

TA的精华主题

TA的得分主题

发表于 2023-2-24 16:42 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
附件,请测试

20230224_规范格式.zip

17.63 KB, 下载次数: 6

TA的精华主题

TA的得分主题

发表于 2023-2-24 16:46 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2023-2-24 17:09 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
数字定长15位,替换、重组都能解决

TA的精华主题

TA的得分主题

发表于 2023-2-24 21:58 | 显示全部楼层
Function CheckNum(X$)
Dim Y$, T$
If InStr(X, "-") Then Y = "-"
X = Replace(Replace(X, "-", ""), ".", ",")
T = Right(X, 3)
If T Like ",##" Then X = Replace(X & "/", T & "/", "." & Mid(T, 2))
CheckNum = Format(Y & X, "#,##0.00")
End Function

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2023-2-25 01:30 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
ovxyz 发表于 2023-2-24 16:29
感谢兄弟,运行没有问题。

那个自定义函数问题很大。
你大意了,没认真与源数据比较。
我花时间写了代码,你把待处理的“原始文件.txt”放在桌面上,然后运行下列代码试试:
Sub ljljlj()
    Dim theStr$, my_TextFileFullName$, a As Variant, i&, j&
    Dim theMatches As Object, theMatch As Variant, reg As Object
    '
    my_TextFileFullName = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\原始文件.txt"
    With CreateObject("ADODB.Stream")
        .Type = 2
        .Mode = 3
        .Open
        .LoadFromFile my_TextFileFullName
        .Charset = "UTF-8"
        .Position = 2
        theStr = .ReadText
        .Close
    End With
    Set reg = CreateObject("VBScript.RegExp")
    With reg
        .Global = True
        .MultiLine = True
        .Pattern = "^([^\r\n]*)(\r?\n|\r)?"
        Set theMatches = .Execute(theStr)
    End With
    '
    reg.Pattern = "\s"
    With Sheet1
        For Each theMatch In theMatches
            theStr = theMatch.submatches(0)
            If reg.test(theStr) Then theStr = reg.Replace(theStr, "")
            '
            If InStr(1, theStr, "|") > 0 Then
                a = Split(theStr, "|")
                If Left(a(0), 1) <> "-" Then
                    i = i + 1
                    For j = 0 To UBound(a)
                        theStr = a(j)
                        If InStr(1, theStr, ",") > 0 Or InStr(1, theStr, ".") > 0 Then theStr = my_replace(theStr)
                        .Cells(i, j + 1) = theStr
                    Next j
                End If
            End If
        Next theMatch
        .Cells(1).CurrentRegion.Columns.AutoFit
    End With
    Set reg = Nothing
End Sub

Function my_replace(theStr$) As Double
    Dim reg As Object
    '
    theStr = Replace(theStr, ".", ",")
    Set reg = CreateObject("VBScript.RegExp")
    With reg
        .Global = True
        .Pattern = "[^\d\.,-]"
        If .test(theStr) Then theStr = .Replace(theStr, "")
        .Pattern = "-$"
        If .test(theStr) Then theStr = "-" & .Replace(theStr, "")
        .Pattern = ",(?=\d{2}[^\d]*$)"
        If .test(theStr) Then theStr = .Replace(theStr, ".")
    End With
    my_replace = theStr
    Set reg = Nothing
End Function

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2023-2-25 21:57 | 显示全部楼层
gbgbxgb 发表于 2023-2-25 01:30
那个自定义函数问题很大。
你大意了,没认真与源数据比较。
我花时间写了代码,你把待处理的“原始文件 ...

代码太长了
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-29 22:21 , Processed in 0.037863 second(s), 7 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表