帮忙编个小程序,本人不胜感激

haohaohappy 2004-10-14 10:51:56
有字符串
AVB4-1
AB5-36
本人想通过一个程序转为同一个格式的,主要是数字部分,字母部分不变
转为:
AVB04-001
AB05-036
本人已编一个,但总觉得不够简单,看大家有没有什么好方法。
...全文
223 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶帆 2004-10-14
  • 打赏
  • 举报
回复
'按楼上的思路写了点代码,不敢保证是最简单的
Private Sub Command1_Click()
Dim strData(0 To 1) As String
Dim strTemp() As String
Dim i As Long, j As Long

strData(0) = "AVB4-1"
strData(1) = "AB5-36"

For i = 0 To UBound(strData)
strTemp = Split(strData(i), "-")
strTemp(1) = Format(strTemp(1), "000")
For j = Len(strTemp(0)) To 1 Step -1
If IsNumeric(Mid(strTemp(0), j, 1)) = False Then
strTemp(0) = Left(strTemp(0), j) & Format(Val(Right(strTemp(0), Len(strTemp(0)) - j)), "00")
Exit For
End If
Next
strData(i) = strTemp(0) & "-" & strTemp(1)
Debug.Print strData(i)
Next

End Sub
aiur2000 2004-10-14
  • 打赏
  • 举报
回复
先split‘-’分两个数
然后处理一下后面的数
判断左起是数字的位数开始位分出左边的数
数字位数定位format(a,"000")给字符 '定义固定3位
最后连接起来
haohaohappy 2004-10-14
  • 打赏
  • 举报
回复
谢谢大家
northwolves 2004-10-14
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
MsgBox trans("AB5-36") & vbCrLf & trans("AvB4-1")
End Sub
Function trans(ByVal x As String) As String
Dim a(2) As String
a(0) = Left(x, InStr(x, "-") - 1)
a(1) = IIf(IsNumeric(Right(a(0), 2)), Right(a(0), 2), Right(a(0), 1))
a(0) = Left(a(0), Len(a(0)) - Len(a(1)))
a(1) = Right("00" & a(1), 2) & "-"
a(2) = Right("000" & Mid(x, InStr(x, "-") + 1), 3)
trans = Join(a, "")
End Function
guxizhw 2004-10-14
  • 打赏
  • 举报
回复
叶帆都来了阿,不敢出手了:(
haohaohappy 2004-10-14
  • 打赏
  • 举报
回复
northwolves(狼行天下)
转换下面的就不行了:
MsgBox trans("AB15-36") & vbCrLf & trans("AvB14-11")
MY2000 2004-10-14
  • 打赏
  • 举报
回复
UP,我也顶!
caoyisheng 2004-10-14
  • 打赏
  • 举报
回复
偶也来学习一下!^_^!!
jam021 2004-10-14
  • 打赏
  • 举报
回复
关注,帮你顶!
northwolves 2004-10-14
  • 打赏
  • 举报
回复
Private Sub Command2_Click()
MsgBox trans("AB5-36") & vbCrLf & trans("AvB4-1")
End Sub


Function trans(ByVal x As String) As String
Dim temp As String
temp = Left(x, InStr(x, "-")) & " "
Mid(temp, Len(temp) - 2) = "0" & Trim(Right(temp, 3))
trans = Format(Mid(x, InStr(x, "-") + 1), "000")
trans = temp & trans
End Function
libralibra 2004-10-14
  • 打赏
  • 举报
回复
高手都出来啦?
等等,我搬个凳子学习一下。
starsoulxp 2004-10-14
  • 打赏
  • 举报
回复
顶楼上。

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧