excel 将一个数组的输入分配给两个/多个其他数组

oalqel3c  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(92)

我想知道是否有人知道基于数组位置检索数组输入的语法。很高兴知道在一般情况下,而且,在我的场合,它是具体的这种方式,我想使用它:我希望一行文本在达到一定长度时被打断,并在某些分隔符处“feed line - vbLF”,而不仅仅是在一定长度处,从而破坏单词。
这是我的尝试,但我希望代码以下面最远的形式编写:
对不起,这个网站上的语法不好,不管怎样,这里是:

If Len(strListOfNames) > 36 Then

    arrstrSplit = Split(strListOfNames, ",") 'delimiter could have been "space" = " " too for general text and not lists
    For arrElmntNum = 0 To UBound(arrstrSplit)
        intLenStrName = Len(arrstrSplit(arrElmntNum))
        If (intLenStrNamesPlural + intLenStrName) < 36 Then
            intLenStrNamesPlural = intLenStrNamesPlural + intLenStrName
            arrFrstNamesPlural.Add (arrstrSplit(arrElmntNum))
        Else
            ReDim arrFrstNamesPlural(0 To arrElmntNum - 1)
        
            ReDim arrScndNamesPlural(0 To (UBound(arrstrSplit) - arrElmntNum))
        
            For FrstArrElmntNum = 0 To (arrElmntNum - 1)
                arrFrstNamesPlural(FrstArrElmntNum) = arrstrSplit(FrstArrElmntNum)
                strFrstNamesPlural = Join(arrFrstNamesPlural, ", ")
            Next
        
            For ScndArrElmntNum = arrElmntNum To UBound(arrstrSplit)
                arrScndNamesPlural(ScndArrElmntNum - arrElmntNum) = arrstrSplit(ScndArrElmntNum)
                arrScndNamesPlural = Join(arrScndNamesPlural, ", "):
            Next

        End If
    Next
End If

字符串
我的意图-希望是最好的解决方案:

If Len(strListOfNames) > 36 Then

    arrstrSplit = Split(strListOfNames, ",")
    For arrElmntNum = 0 To UBound(arrstrSplit)
        intLenStrName = Len(arrstrSplit(arrElmntNum))
        If (intLenStrNamesPlural + intLenStrName) < 36 Then
            intLenStrNamesPlural = intLenStrNamesPlural + intLenStrName
        Else
            arrFrstBorholesStr(0 to (arrElmntNum-1)) = arrstrSplit(0 to (arrElmntNum-1))
            arrFrstBorholesStr(0 to (UBound(arrstrSplit) - arrElmntNum) = arrstrSplit(arrElmntNum to UBound(arrstrSplit))
           GoTo SplitIsDone_CheckIsComplete
        End If
    Next
End If

SplitIsDone_CheckIsComplete:  'Jumps out of the for-loop.

zpqajqem

zpqajqem1#

我不确定我是否完全理解了你的问题,但是下面的代码将接受一个字符串,并根据你指定的input_delimiter将它拆分成一个数组。然后,它将构建一个字符串数组,将第一个数组中的元素组合到指定的max_line_length。output_array中的元素由指定的output_delimiter分隔。
以下列表:
阿尔巴尼亚、安道尔、奥地利、白俄罗斯、比利时、波斯尼亚和黑塞哥维那、保加利亚、克罗地亚、捷克、丹麦、爱沙尼亚、芬兰、法国、德国、希腊、罗马教廷、匈牙利、冰岛、爱尔兰、意大利、拉脱维亚、列支敦士登、立陶宛、 lucene 堡、马耳他、摩尔多瓦、摩纳哥、黑山、荷兰、北马其顿、挪威、波兰、葡萄牙、罗马尼亚、俄罗斯、圣马力诺、塞尔维亚、斯洛伐克、斯洛文尼亚、西班牙、瑞典、瑞士、乌克兰、英国
可以转换成数组:

Albania-Andorra-Austria-Belarus-Belgium
Bosnia and Herzegovina-Bulgaria-Croatia-Czechia
Denmark-Estonia-Finland-France-Germany-Greece
Holy See-Hungary-Iceland-Ireland-Italy-Latvia
Liechtenstein-Lithuania-Luxembourg-Malta-Moldova
Monaco-Montenegro-Netherlands-North Macedonia
Norway-Poland-Portugal-Romania-Russia-San Marino
Serbia-Slovakia-Slovenia-Spain-Sweden-Switzerland
Ukraine-United Kingdom

字符串
代码如下:

Sub test_array()
  Dim x As Long
  Dim new_array() As String
  Const text = "Albania,Andorra,Austria,Belarus,Belgium,Bosnia and Herzegovina,Bulgaria,Croatia,Czechia,Denmark,Estonia,Finland,France,Germany,Greece,Holy See,Hungary,Iceland,Ireland,Italy,Latvia,Liechtenstein,Lithuania,Luxembourg,Malta,Moldova,Monaco,Montenegro,Netherlands,North Macedonia,Norway,Poland,Portugal,Romania,Russia,San Marino,Serbia,Slovakia,Slovenia,Spain,Sweden,Switzerland,Ukraine,United Kingdom"
  new_array = array_organizer(text, 50, ",", "-")
  For x = LBound(new_array) To UBound(new_array)
    Debug.Print new_array(x)
  Next
End Sub

Function array_organizer(text As String, max_line_length As Long, Optional input_delimiter As String = "", Optional output_delimiter As String = "")
    Dim one_line As String
    Dim x As Integer
    Dim output_collection As New Collection
    Dim output_array() As String
    Dim items() As String
    items = Split(text, input_delimiter)
    
    For x = LBound(items) To UBound(items)
      one_line = items(x)
      
      ' key trying to add the next item to the current line as long as we have items
      Do Until x + 1 > UBound(items)

           ' check to see if there is room on the current line to add the next item, if not we exit loop
           If Len(one_line) + Len(items(x + 1)) + Len(output_delimiter) > max_line_length Then Exit Do
           
           ' if we made it here, there is room to add the next item to the current line
           'add the next item to the current line including an output delimiter
           one_line = one_line & output_delimiter & items(x + 1)
            x = x + 1 ' move to the next item
           
           DoEvents 'this will allows us to interrupt the code if a bug puts us into an endless loop
      Loop
      
      'were are done building the current line, add it to the output colllection
      output_collection.Add one_line
      
    Next
    
    ' the requirements are to return an array, so will size the array then copy the collection to the array
    ReDim output_array(0 To output_collection.Count - 1)
    For x = 1 To output_collection.Count
      output_array(x - 1) = output_collection(x)
    Next
    array_organizer = output_array
    
    ' if the we are allowed to retrun a collection instead of an array, delete the prior block and enable this line
    'array_organizer = output_collection

End Function


在“organize_array”函数中,我使用一个集合来累积行,而不是为每个新行重新调暗output_array,因为重新调暗数组确实效率很低

相关问题