计算单元格内的分裂数量

问题描述 投票:0回答:1

好的,我正在寻找一个 VBA 函数,它会根据分割字符长度和最接近的 COMMA 分割值告诉我一个单元格内需要多少次分割

好的,我有一个这样的单元格(A33),

N2NP002、N3NP001、N4NP027、N5NP012、N6NP003、N10NP010、Z401、Z217、Z218、Z219、Z220、Z2、Z22、Z223、Z224、Z225、Z226、Z227、Z228、Z229、Z230、Z231 ,Z232,Z233,Z234 ,Z235,Z236,Z33,Z237,Z238,Z239,Z240,Z241,Z222,Z223,Z224,Z225,Z226,Z227,Z228,Z229,Z23

根据以下标准将其拆分为 3 个单元格,以逗号拆分,保持长度接近 76 个字符。

我有一个例程,将其拆分如下,

N2NP002,N3NP001,N4NP027,N5NP012,N6NP003,N10NP010,Z401,Z217,Z218,Z219,Z220,Z2  (Which is 76 Characters)   

Z22,Z223,Z224,Z225,Z226,Z227,Z228,Z229,Z230,Z231,Z232,Z233,Z234,Z235,Z236                (73 Characters)   

Z33,Z237,Z238,Z239,Z240,Z241,Z222,Z223,Z224,Z225,Z226,Z227,Z228,Z229,Z23                (72 Characters)   

我想要的是一个 VBA 函数,它会告诉我我的分割例程需要 3 个单元。

ie

=SplitNumber(A33,76)
其中 A33 是输入文本,76 是字符长度并以逗号分隔。

因此,在给出的示例中,我希望看到

=SplitNumber(A33,76)
的结果为 3

记住这里的问题是,当第 77 个字符是逗号时,分割发生在第 76 个字符处。 即,查找字符 77 处或之前的最后一个逗号,然后该值 -1 是您要拆分的值,根据发生 Z2 的第一次拆分,其中逗号位于位置 77。如果 Z2 是 Z12,则 Z12 将被推入下一行和效果流将是 SplitNumber(A33,76) 将得到 4。

当 Z2 变为 Z12 时我的例程将给出的示例结果,

N2NP002,N3NP001,N4NP027,N5NP012,N6NP003,N10NP010,Z401,Z217,Z218,Z219,Z220                 (73 Characters)   

Z12,Z22,Z223,Z224,Z225,Z226,Z227,Z228,Z229,Z230,Z231,Z232,Z233,Z234,Z235                 (72 Characters)   

Z236,Z33,Z237,Z238,Z239,Z240,Z241,Z222,Z223,Z224,Z225,Z226,Z227,Z228,Z229                 (73 Characters)   

Z23(3 个字符)

如果感兴趣,或者如果有帮助,这里是我必须将 A33 拆分为 76 个字符长度的 Sub,根据需要将结果放入单元格 C33、C34、C35 等

Sub SplitTextBy76Chars() 

    Dim inputText As String 
    Dim maxLen As Integer 
    Dim result As String 
    Dim lastComma As Integer
    Dim cellRow As Integer 
    Dim checkChar As String      

    ' Read the input text from A33
    inputText = Range("A33").Value
    maxLen = 76 ' Maximum length for each split 
    cellRow = 33 ' Starting row for output, adjust as needed 
  
    Do While Len(inputText) > 0 
        ' Get the first maxLen characters
        result = Left(inputText, maxLen)          

        ' Check if the 77th character is a comma
        If Len(inputText) > maxLen Then
            checkChar = Mid(inputText, maxLen + 1, 1) 

            If checkChar = "," Then 
                ' If the 77th character is a comma, use the first 76 characters
                result = Left(inputText, maxLen)
                inputText = Mid(inputText, maxLen + 2)
            Else 
                ' Find the last comma within this substring
                lastComma = InStrRev(result, ",") 
                ' If a comma is found, split there; otherwise, take the full 76 characters
                If lastComma > 0 Then
                    result = Left(result, lastComma - 1) 
                    inputText = Mid(inputText, lastComma + 1) 
                Else 
                    inputText = Mid(inputText, maxLen + 1) 
                End If 

            End If 
        Else 
            inputText = "" 
        End If         

        ' Write the result to the next row 
        Range("C" & cellRow).Value = result
        cellRow = cellRow + 1 

    Loop 
End Sub
excel vba excel-2010
1个回答
0
投票

解决方案:

Public Function SplitNumber(inputText As Range, Optional maxLen As Integer = 76)
    Dim x As Integer, dic As Object, splVal, dkey
    Set dic = CreateObject("Scripting.Dictionary")
    x = 1
    For Each splVal In Split(inputText.Value, ",")
        If dic.Exists(x) Then
            If Len(dic(x) & "," & splVal) <= maxLen Then
                dic(x) = dic(x) & "," & splVal
            Else
                x = x + 1
                dic.Add x, splVal
            End If
        Else
            dic.Add x, splVal
        End If
    Next splVal
    x = Application.Caller.Row - inputText.Row + 1
    If dic.Exists(x) Then
        SplitNumber = dic(x)
    Else
        SplitNumber = ""
    End If
End Function

测试:

© www.soinside.com 2019 - 2024. All rights reserved.