Hey there!
I'm struggling at the moment to make a piece of VBA that will allow me "bubble sort" and re-arrange the internal characters within a string
E.g. String "BAA" will become "AAB"
Below is my feeble attempt at the VBA code for this
Public Function sortwdaz(txtstring1 As String)
Dim str1Len As Integer
Dim ctr As Long
Dim currentpos As Single
Dim tempstr As String
Dim currentcharind As Long
Dim nextcharind As Long
str1Len = Len(txtstring1)
tempstr = ""
ctr = 0
currentcharind = 1
nextcharind = 0
For ctr = 1 To str1Len - 1
currentcharind = Asc(Mid(txtstring1, ctr, 1))
nextcharind = Asc(Mid(txtstring1, ctr + 1, 1))
If currentcharind <= nextcharind Then
tempstr = tempstr & Mid(txtstring1, ctr, 1) & Mid(txtstring1, ctr + 1, 1)
Else
tempstr = tempstr & Mid(txtstring1, ctr + 1, 1) & Mid(txtstring1, ctr, 1)
ctr = ctr + 1
End If
Next
sortwdaz = tempstr
End Function
Any advice etc would be greatly appreciated!
I'm struggling at the moment to make a piece of VBA that will allow me "bubble sort" and re-arrange the internal characters within a string
E.g. String "BAA" will become "AAB"
Below is my feeble attempt at the VBA code for this
Public Function sortwdaz(txtstring1 As String)
Dim str1Len As Integer
Dim ctr As Long
Dim currentpos As Single
Dim tempstr As String
Dim currentcharind As Long
Dim nextcharind As Long
str1Len = Len(txtstring1)
tempstr = ""
ctr = 0
currentcharind = 1
nextcharind = 0
For ctr = 1 To str1Len - 1
currentcharind = Asc(Mid(txtstring1, ctr, 1))
nextcharind = Asc(Mid(txtstring1, ctr + 1, 1))
If currentcharind <= nextcharind Then
tempstr = tempstr & Mid(txtstring1, ctr, 1) & Mid(txtstring1, ctr + 1, 1)
Else
tempstr = tempstr & Mid(txtstring1, ctr + 1, 1) & Mid(txtstring1, ctr, 1)
ctr = ctr + 1
End If
Next
sortwdaz = tempstr
End Function
Any advice etc would be greatly appreciated!