Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 二维数组或数组的数组VBA_Arrays_Ms Access_Vba - Fatal编程技术网

Arrays 二维数组或数组的数组VBA

Arrays 二维数组或数组的数组VBA,arrays,ms-access,vba,Arrays,Ms Access,Vba,是否可以在VBA中执行此操作。我有一个字符串,例如“16A,14B,16E,16C,14D”,我想生成一个数组,看起来像“14,B,D”,“16,a,C,E”。这些字母是从A到E的,不会重复。我有点坚持只生产最后一块。也许我的方法完全错了 Sub Test() Dim myStr As String Dim myStrA As String Dim myStrN As String Dim FormName As String Dim ControlName As String Dim myAr

是否可以在VBA中执行此操作。我有一个字符串,例如“16A,14B,16E,16C,14D”,我想生成一个数组,看起来像“14,B,D”,“16,a,C,E”。这些字母是从A到E的,不会重复。我有点坚持只生产最后一块。也许我的方法完全错了

Sub Test()
Dim myStr As String
Dim myStrA As String
Dim myStrN As String
Dim FormName As String
Dim ControlName As String
Dim myArray() As String

    'Creating a array list
    Set arr = CreateObject("System.Collections.ArrayList")

    'string with values, delimited by comma
    myStr = "16A,14B,16C,14D,16E"          ' => "16,A,C,E" "14,B,D"

    'split string into array of substrings
    myArray = Split(myStr, ",") '=> "16A","14B","16C",",14D","16E"

    ' adding the elements in the array to array_list
    For Each element In myArray
        arr.Add element
    Next

    'sorting happens
    arr.Sort

    'converting ArrayList to an array
    'so now a sorted array of elements is stored in the array sorted_array.
    sorted_array = arr.toarray '=> "14B","14D","16A","16C","16E"

    'concatenate all elements of array into one string
    myStr = Join(sorted_array, ",") '=> "14B,14D,16A,16C,16E"

    'remove letters
    myStrN = StripNumber(myStr) '=> "14,14,16,16,16"

    'remove dublipates
    myStrN = DeDupString(myStrN, ",") '=> "14,16"

    'stip text
    myStrA = StripText(myStr) '=> "B,D,A,C,E"

    PageCount = countSeparators(myStrN, ",")


    [Forms]![frm_LoanEdit2_Print_HYP]![txt_Company] = myStr 'myStrN & "-" & PageCount
    [Forms]![frm_LoanEdit2_Print_HYP]![txt_Bullets] = myStrN


    'display array elements
    For i = 0 To PageCount - 1
        FormName = "frm_LoanEdit2_Print_HYP"
        ControlName = "txt_Page" & i + 1
        Forms(FormName).Controls(ControlName) = sorted_array(LBound(sorted_array) + i)
    Next i
End Sub
我使用这些函数试图得到结果

Function StripText(str As String) As String
  For i = 1 To Len(str)
    B = Mid(str, i, 1)
    Select Case B
      Case "a" To "z", "A" To "Z", ","
        StripText = StripText & B
    End Select
  Next
End Function

Function StripNumber(str As String) As String
  For i = 1 To Len(str)
    B = Mid(str, i, 1)
    Select Case B
      Case "0" To "9", ","
        StripNumber = StripNumber & B
    End Select
  Next
End Function

Function DeDupString(ByVal sInput As String, Optional ByVal sDelimiter As String = ",") As String
'remove duplicate in string

    Dim varSection As Variant
    Dim sTemp As String

    For Each varSection In Split(sInput, sDelimiter)
        If InStr(1, sDelimiter & sTemp & sDelimiter, sDelimiter & varSection & sDelimiter, vbTextCompare) = 0 Then
            sTemp = sTemp & sDelimiter & varSection
        End If
    Next varSection

    DeDupString = Mid(sTemp, Len(sDelimiter) + 1)
End Function

在没有任何排序的情况下,我能够使用VBA
Collection
对象合并数据

Option Explicit

Public Sub SO_TestCol()

    Dim inp As String
    inp = "16A,14B,16E,16C,14D"

    Dim parts() As String
    parts = SplitAtTokens(inp, ",")

    Dim col As New Collection
    Dim item As Variant, code As String, temp As String
    For Each item In parts
        code = Right(item, 1)
        item = Mid(item, 1, Len(item) - 1)
        If KeyExists(col, item) Then
            temp = col(item) & "," & code
            col.Remove (item)
            col.Add temp, item
        Else
            temp = CStr(item) & "," & code
            col.Add temp, item
        End If
    Next

    For Each item In col
        Debug.Print item
    Next
    ' Output:
    ' 16,A,E,C
    ' 14,B,D

End Sub

'---------------------------------------------------------------------------------------
' Procedure : KeyExists
' Author    : ja72
' Date      : 10/2/2017
' Purpose   : Check to see if a key is present in a collection
'---------------------------------------------------------------------------------------
'
Public Function KeyExists(ByVal col As Collection, ByVal key As Variant) As Boolean
    Dim item As Variant
    On Error GoTo NotFound:
    item = col(key)
    On Error GoTo 0
    KeyExists = True
    Exit Function
NotFound:
    KeyExists = False
End Function

'---------------------------------------------------------------------------------------
' Procedure : SplitAtTokens
' Author    : ja72
' Date      : 10/2/2017
' Purpose   : Splits a string into an array of strings, delimeted by a token
'---------------------------------------------------------------------------------------
'
Function SplitAtTokens(ByVal str As String, ByVal tok As String) As String()
    Dim pos, i, num_of_lines, next_token As Integer
    Dim res() As String
    If Not str = vbNullString Then
        num_of_lines = CountInstances(str, tok) + 1
        ReDim res(num_of_lines - 1) As String
        For i = 1 To num_of_lines
            pos = InStr(1, str, tok, vbTextCompare)
            If pos > 0 Then
                res(i - 1) = SplitAt(str, pos - 1)
                str = Right(str, Len(str) - Len(tok))
            Else
                res(i - 1) = str
                str = ""
            End If
        Next i
    End If
    SplitAtTokens = res
End Function

'---------------------------------------------------------------------------------------
' Procedure : CountInstances
' Author    : ja72
' Date      : 10/2/2017
' Purpose   : Counts the instances that a token appears in a string
'             For example `CountInstances("AA-UZ1-0FA2", "A") = 3`
'---------------------------------------------------------------------------------------
'
Function CountInstances(ByVal str As String, ByVal tok As String) As Integer
    Dim res, pos As Integer
    res = 0
    pos = 0
    Do
        pos = InStr(pos + 1, str, tok, vbTextCompare)
        res = res + 1
    Loop Until pos = 0
    CountInstances = res - 1
End Function

'---------------------------------------------------------------------------------------
' Procedure : SplitAt
' Author    : ja72
' Date      : 10/2/2017
' Purpose   : Splits a single string into two strings based on location
'             The first half is returned, the second is assigned to `str`
'---------------------------------------------------------------------------------------
'
Function SplitAt(ByRef str As String, ByVal at As Integer) As String
    SplitAt = Left(str, at)
    str = Mid(str, at + 1)
End Function

是的,字典可能是最好的选择,数字是键,项目是字母的串联。在VBA中这样做是绝对可能的。在VBA中使用
ArrayList
的原因是什么?VBA有一个本机的
集合
类,它同时充当
ArrayList
字典
。没有任何理由使用ArrayList,这些是我用来解决此特定问题的代码段。让我研究一下集合,看看我能挖掘到什么。你需要创建一个集合,其中键是
14
15
16
等,值也是一个集合,在其中添加“B”,“D”等。最后,如果你愿意,你可以使用一个函数将所有值转换为字符串表示。哇,我印象深刻。是的,很好用。在我的情况下,密钥不需要排序,因此此代码非常完美。非常感谢。