これはVBAで可能ですか? 16A、14B、16E、16C、14Dのような文字列があり、この "14、B、D"、 "16、A、C、E"のような配列を生成したいとします。文字はAからEまであり、繰り返されません。私はちょうど最後の部分を生成するために固執しています。多分私のアプローチは完全に間違っていますか?2D配列または配列の配列VBA
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でこれを行うことは間違いありません – jsotola
VBAで '.NET'の' ArrayList'を使用している理由はありますか? VBAには、 'ArrayList'と' Dictionary'の両方として動作するネイティブ 'Collection'クラスがあります。 – ja72