Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.05 KB | None | 0 0
  1. Sub Sortieren()
  2. '* Dimensionierung der Variablen
  3. Dim wb As Workbook
  4. Dim rng As Range
  5. Dim row As Range
  6. Dim cell As Range
  7. Dim strTeilstring()   As Variant
  8. Dim strTrennzeichen As String
  9.  
  10. '** Trennen von Zellinhalten an einem vorgegebenen Trennzeichen
  11.  
  12. '* Vorgaben definieren
  13. Set wsakt = Worksheets(1)
  14. strTrennzeichen = ", " 'Trennzeichen festlegen z.B. Komma(,) Semikolon(;) Bindestrich(-) etc.
  15. Leerzeichen = " "
  16.  
  17. '* Durchlaufen aller Datenzeilen
  18. LastRow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).row
  19. NextCol = 3 '=> PLZ // 4 => Ort
  20. For x = 3595 To 3636
  21.     y = 2
  22.     DoEvents
  23.    
  24.     stext = Cells(x, y).Value ' den Text holen
  25. Def:
  26.     iPos = InStr(stext, strTrennzeichen)  ' Komma-Position feststellen
  27.     iPosL = InStr(stext, " ") ' Leerzeichen-Position feststellen
  28.  
  29.     If (UBound(Split(stext, strTrennzeichen)) > 2) Then ' wenn Stra?e, Nr., Ort, PLZ, Land
  30. U3Null:
  31.         While (UBound(Split(stext, strTrennzeichen)) > 1)
  32.             If (Len(Left(stext, iPos - 1)) = 5 And IsNumeric(Left(stext, iPos - 1))) Then
  33.                 If IsEmpty(Cells(x, 3).Value) = False Then
  34.                     Cells(x, 4).Value = Cells(x, 3).Value
  35.                     y = 4
  36.                     Cells(x, 3).Value = Left(stext, iPos - 1)
  37.                     stext = Right(stext, Len(stext) - iPos - 1)
  38.                     iPos = InStr(stext, strTrennzeichen)
  39.                     GoTo U3Null
  40.                 Else
  41.                 Cells(x, 3).Value = Left(stext, iPos - 1)
  42.                 End If
  43.             End If
  44.             Cells(x, y).Value = Left(stext, iPos - 1)
  45.             stext = Right(stext, Len(stext) - iPos - 1)             ' Eingabetext "verk?rzen"
  46.  
  47.             iPos = InStr(stext, strTrennzeichen)                    ' Komma suchen
  48.             If (y < 4) Then
  49.                 y = y + 2                                           ' Zeile nach LastRow errechnen
  50.                 Cells(x, y).Value = stext                           ' letztes Wort ?bertragen
  51.             End If
  52.         Wend
  53.     End If
  54.    
  55.     If (UBound(Split(stext, strTrennzeichen)) = 2) Then             ' wenn Stra?e Nr., PLZ Ort, Land
  56. U2Null:
  57.         While (UBound(Split(stext, strTrennzeichen)) > 1)
  58.             Cells(x, y).Value = Left(stext, iPos - 1)               ' Stra?e einf?gen
  59.             stext = Right(stext, Len(stext) - iPos - 1)             ' Eingabetext "verk?rzen"
  60.             iPos = InStr(stext, strTrennzeichen)                    ' Komma suchen
  61.             iPosL = InStr(stext, " ")                               ' Leerzeichen suchen
  62.             If (Len(stext) = 5 And IsNumeric(stext)) Then           ' Falls nur PLZ
  63.                 If IsEmpty(Cells(x, 3).Value) = False Then
  64.                     Cells(x, 4).Value = Cells(x, 3).Value
  65.                     y = 4
  66.                     Cells(x, 3).Value = stext
  67.                     stext = Right(stext, Len(stext) - iPos - 1)
  68.                     iPos = InStr(stext, strTrennzeichen)
  69.                     GoTo U2Null
  70.                 Else
  71.                 Cells(x, 3).Value = Left(stext, iPos - 1)
  72.                 End If
  73.             End If
  74.             If (Len(Left(stext, iPos - 1)) = 5 And IsNumeric(Left(stext, iPos - 1))) Then    ' Falls PLZ 1. Element
  75.                 If IsEmpty(Cells(x, 3).Value) = False Then
  76.                     Cells(x, 4).Value = Cells(x, 3).Value
  77.                     y = 4
  78.                     Cells(x, 3).Value = stext
  79.                     stext = Right(stext, Len(stext) - iPos - 1)
  80.                     iPos = InStr(stext, strTrennzeichen)
  81.                     GoTo U2Null
  82.                 Else
  83.                 Cells(x, 3).Value = Left(stext, iPos - 1)
  84.                 End If
  85.             End If
  86.             If (Len(Right(stext, Len(stext) - iPos - 1)) = 5 And IsNumeric(Right(stext, Len(stext) - iPos - 1))) Then
  87.                 If IsEmpty(Cells(x, 3).Value) = False Then                                  ' Falls PLZ 2. Element
  88.                     Cells(x, 4).Value = Cells(x, 3).Value
  89.                     y = 4
  90.                     Cells(x, 3).Value = stext
  91.                     stext = Right(stext, Len(stext) - iPos - 1)
  92.                     iPos = InStr(stext, strTrennzeichen)
  93.                     GoTo U2Null
  94.                 Else
  95.                 Cells(x, 3).Value = Right(stext, Len(stext) - iPos - 1)
  96.                 Cells(x, 4).Value = Left(stext, iPos - 1)
  97.                 End If
  98.                 GoTo Weiter
  99.             End If
  100.             If (Len(Left(stext, iPosL - 1)) = 5 And IsNumeric(Left(stext, iPosL - 1))) Then
  101.                 If IsEmpty(Cells(x, 3).Value) = False Then  'Wenn Inhalt vorhanden
  102.                     Cells(x, 4).Value = Cells(x, 3).Value
  103.                     y = 4
  104.                     Cells(x, 3).Value = stext
  105.                     stext = Right(stext, Len(stext) - iPos - 1)
  106.                     iPos = InStr(stext, strTrennzeichen)
  107.                     GoTo U2Null
  108.                 Else
  109.                 Cells(x, 3).Value = Left(stext, iPosL - 1)   'PLZ einf?gen
  110.                 End If
  111.                 If (UBound(Split(Left(stext, iPos - 1), " ")) = 1) Then
  112.                     Cells(x, 4).Value = Right(Left(stext, iPos - 1), Len(Left(stext, iPos - 1)) - iPosL) 'Ort einf?gen
  113.                     stext = Right(stext, Len(stext) - iPos - 1)
  114.                     GoTo Def
  115.                 End If
  116.             End If
  117.             Cells(x, 4).Value = Right(stext, Len(stext) - iPos - 1)        ' Verk?rze stext auf Inhalt nach PLZ (!iPosL!)
  118.         Wend
  119.     End If
  120. U1Null:
  121.     If (UBound(Split(stext, strTrennzeichen)) <= 1) Then            ' wenn 1 Element ?brig
  122.         If (UBound(Split(stext, Leerzeichen)) = 3 And UBound(Split(stext, strTrennzeichen)) = 1) Then
  123.             Cells(x, 2).Value = Left(stext, iPos - 1)               '* Str Nr., PLZ Ort *' F?ge Str Nr. ein
  124.             stext = Right(stext, Len(stext) - iPos - 1)             ' K?rze auf PLZ Ort
  125.             iPos = InStr(stext, strTrennzeichen)                    ' Komma suchen
  126.             iPosL = InStr(stext, " ")                               ' Leerzeichen suchen
  127.             Cells(x, 3).Value = Left(stext, iPosL - 1)
  128.             Cells(x, 4).Value = Right(stext, Len(stext) - iPosL)
  129.             'Cells(x, 4).Value = Right(Left(stext, iPos - 1), Len(Left(stext, iPos - 1)) - iPosL)
  130.         End If
  131.         If (iPos > 0) Then  ' Else If
  132.             If (Len(Left(stext, iPos - 1)) = 5 And IsNumeric(Left(stext, iPos - 1))) Then
  133.                 Cells(x, 3).Value = Left(stext, iPos - 1)           ' wenn PLZ
  134.             Else
  135.                 Cells(x, 4) = Left(stext, iPos - 1)                 ' wenn Ort
  136.             End If
  137.         End If
  138.     End If
  139.                            
  140. '  '* Teilstring am Delimiter auslesen
  141. '    strTeilstring = Split(Trim(wsakt.Cells(x, 2).Value), strTrennzeichen)
  142. '
  143. '  '* Durchlaufen des gesamten Arrays einer Zelle vom ersten bis zum letzten Wert
  144. '  For a = LBound(strTeilstring) To UBound(strTeilstring)
  145. '
  146. '    '* Array-Elemente nacheinander eintragen
  147. '    wsakt.Cells(x, NextCol).Value = Trim(strTeilstring(a))
  148. '
  149. '    '* Zeilenz?hler erh?hen
  150. '    NextCol = NextCol + 1
  151. '  Next a
  152. Weiter:
  153. Next x
  154.  
  155. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement