How can I clean up this code? It works but it resembles a plate of spaghetti. Any advice is welcome. The goal of the function is to parse strOrig into Expected Results. Of course strOrig will vary, but will always be in the same format.
Code:
Function ParseFolders()
Dim strOrig As String
Dim strSemi As String
Dim intSemi As Integer, intSemiBegDoc As Integer
Dim strPipe As String
Dim intPipe As Integer
Dim arTag() As String
Dim arField() As String
Dim arSemi() As Integer
Dim arPipe() As Integer
Dim intLoopCount As Integer, intSecondLoopCount As Integer
Dim strBegDoc As String, strFolder As String
Dim i As Integer, j As Integer, intFieldLen As Integer
Dim strFullString() As String, strDelim As String
strDelim = "[delim]"
strSemi = ";"
strPipe = "|"
strOrig = "AB000001;|Folder01|Tag01|Tag02|;|Folder02|Tag01|Tag03|;"
' Expected results: AB000001;Folder01[delim]Tag01;Folder01[delim]Tag02;Folder02[delim]Tag01;Folder02[delim]Tag03
intSemiBegDoc = InStr(1, strOrig, strSemi)
strBegDoc = Left(strOrig, InStr(intSemiBegDoc, strOrig, strSemi) - 1) & strSemi
intSemi = intSemiBegDoc
Do While intSemi < Len(strOrig)
intSemi = InStr(intSemi + 1, strOrig, strSemi)
intLoopCount = intLoopCount + 1
Loop
ReDim arField(intLoopCount - 1)
ReDim arSemi(intLoopCount)
ReDim arField(intLoopCount - 1)
intSemi = intSemiBegDoc
arSemi(0) = intSemiBegDoc
intSecondLoopCount = 1
Do While intSemi < Len(strOrig)
intSemi = InStr(intSemi + 1, strOrig, strSemi)
arSemi(intSecondLoopCount) = intSemi
intSecondLoopCount = intSecondLoopCount + 1
Loop
Dim intUbound As Integer
intUbound = UBound(arSemi())
For i = 0 To intUbound - 1
If i < intUbound Then
arField(i) = Mid(strOrig, arSemi(i) + 1, (arSemi(i + 1) - arSemi(i)))
Else
arField(i) = Mid(strOrig, arSemi(i - 1) + 1, arSemi(i))
End If
Next
'Testing for pipes in fields************
Dim strTest As String
ReDim strFullString(UBound(arField()))
For j = 0 To UBound(arField())
strTest = arField(j)
intLoopCount = 0
intPipe = 1
Do While intPipe < Len(strTest) - 1
intPipe = InStr(intPipe + 1, strTest, strPipe)
intLoopCount = intLoopCount + 1
Loop
ReDim arPipe(intLoopCount - 1)
intUbound = UBound(arPipe())
intLoopCount = 0
intPipe = 1
Do While intPipe < Len(strTest) - 1
intPipe = InStr(intPipe + 1, strTest, strPipe)
arPipe(intLoopCount) = intPipe
intLoopCount = intLoopCount + 1
Loop
ReDim arTag(intLoopCount - 2)
intUbound = UBound(arTag())
strFolder = Left(strTest, arPipe(0))
For i = 0 To intUbound
If i < intUbound Then
arTag(i) = Mid(strTest, arPipe(i), (arPipe(i + 1) - arPipe(i) + 1))
Else
arTag(i) = Mid(strTest, arPipe(i), Len(strTest) - arPipe(i))
End If
Next
If j = 0 Then
strFullString(j) = strBegDoc
End If
For i = 0 To intUbound
strFullString(j) = strFullString(j) & strFolder & strDelim & arTag(i) & strSemi
Next i
Next j
strTest = ""
For i = 0 To UBound(strFullString())
strTest = strTest & strFullString(i)
Next i
Debug.Print strTest
End Function
Comment