EXCEL 电子位号 补全 如(R83-85)

该博客主要围绕 Excel 补全电子位号展开,如将类似(R83 - 85)的形式进行补全,借助 Excel 工具可高效完成电子位号的补全工作,在信息技术领域有一定应用价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sub 按钮1_Click()
    Dim strD As String
    Dim globalIndex As Integer
    globalIndex = 2
    Set mysheet1 = ThisWorkbook.Worksheets("Sheet3")
    While mysheet1.Cells(globalIndex, 4) <> ""
         strD = mysheet1.Cells(globalIndex, 4)
      mysheet1.Cells(globalIndex, 4) = FormatNUM(strD)
        globalIndex = globalIndex + 1
    Wend

End Sub


Public Function FormatNUM(key As String) As String
    FormatNUM = ""
    Dim nKey As String
    Dim sStart As String
    Dim sEnd As String
    Dim indexInt As Integer
    
    aArray = Split(key, " ")
    For i = 0 To UBound(aArray)
        Item = aArray(i)
        bArray = Split(Item, "-")
       If UBound(bArray) > 0 Then '有-的处理没有就不处理
            sStart = bArray(0)
            sEnd = bArray(1)
            indexInt = index(sStart)
            Prefix = Mid(sStart, 1, indexInt - 1) '前缀
            StartInt = CInt(Mid(sStart, indexInt)) '开始位号
            EndInt = CInt(sEnd) '结束位号
            'MsgBox (StartInt)
            'MsgBox (EndInt)
            'MsgBox (Prefix)
            Dim nextPlace As Integer
            FormatNUM = FormatNUM & sStart & ","
            For j = StartInt + 1 To EndInt
                 If j = EndInt Then
                    FormatNUM = FormatNUM & Prefix & CStr(j)
                 Else
                    FormatNUM = FormatNUM & Prefix & CStr(j) & ","
                 End If
                 
            Next j
            'MsgBox (bArray)
       Else
            If i = UBound(aArray) Then
                FormatNUM = FormatNUM & Item
            Else
                FormatNUM = FormatNUM & Item & ","
            End If
             
       End If
    Next i
    
End Function


Public Function index(firstStr As String) As Integer
    Dim lastBeginIndex As Boolean '是否是前缀的数字
    lastBeginIndex = True
    Dim strNum As String
    For i = 1 To Len(firstStr)
        strNum = Mid(firstStr, i, 1)
      
        
            If IsNumeric(strNum) Then '这个方法找是否为数字字符找到数字的位置
                If lastBeginIndex Then
                index = i
                lastBeginIndex = False
                End If
            Else
                lastBeginIndex = True
            End If
        
    Next i
End Function

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值