Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.31 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.  
  16. '* Durchlaufen aller Datenzeilen
  17. LastRow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).row
  18. NextCol = 3 '=> PLZ // 4 => Ort
  19. For x = 1769 To 1770
  20.     y = 2
  21.     DoEvents
  22.    
  23.     stext = Cells(x, y).Value ' den Text holen
  24.     iPos = InStr(stext, strTrennzeichen)  ' Komma-Position feststellen
  25.     iPosL = InStr(stext, " ") ' Leerzeichen-Position feststellen
  26.  
  27.     If (UBound(Split(stext, strTrennzeichen)) > 2) Then ' wenn Stra?e, Nr., Ort, PLZ, Land
  28. U3Null:
  29.         While (iPos > 0)
  30.             If (Len(Left(stext, iPos - 1)) = 5 And IsNumeric(Left(stext, iPos - 1))) Then
  31.                 If IsEmpty(Cells(x, 3).Value) = False Then
  32.                     Cells(x, 4).Value = Cells(x, 3).Value
  33.                     y = 4
  34.                     Cells(x, 3).Value = Left(stext, iPos - 1)
  35.                     stext = Right(stext, Len(stext) - iPos - 1)
  36.                     iPos = InStr(stext, strTrennzeichen)
  37.                     GoTo U3Null
  38.                 Else
  39.                 Cells(x, 3).Value = Left(stext, iPos - 1)
  40.                 End If
  41.             End If
  42.             Cells(x, y).Value = Left(stext, iPos - 1)
  43.             stext = Right(stext, Len(stext) - iPos - 1)                    ' Eingabetext "verk?rzen"
  44.  
  45.             iPos = InStr(stext, strTrennzeichen)                                    ' Komma suchen
  46.             If (y < 4) Then
  47.                 y = y + 1                                       ' Zeile nach LastRow errechnen
  48.                 Cells(x, y).Value = stext                             ' letztes Wort ?bertragen
  49.             End If
  50.         Wend
  51.     End If
  52.    
  53.     If (UBound(Split(stext, strTrennzeichen)) = 2) Then ' wenn Stra?e Nr., PLZ, Ort, Land
  54. U2Null:
  55.         While (iPos > 0)
  56.             Cells(x, y).Value = Left(stext, iPos - 1)
  57.             stext = Right(stext, Len(stext) - iPos - 1)                    ' Eingabetext "verk?rzen"
  58.             iPos = InStr(stext, strTrennzeichen)                                    ' Komma suchen
  59.             If (Len(stext) = 5 And IsNumeric(stext)) Then
  60.                 If IsEmpty(Cells(x, 3).Value) = False Then
  61.                     Cells(x, 4).Value = Cells(x, 3).Value
  62.                     y = 4
  63.                     Cells(x, 3).Value = stext
  64.                     stext = Right(stext, Len(stext) - iPos - 1)
  65.                     iPos = InStr(stext, strTrennzeichen)
  66.                     GoTo U2Null
  67.                 Else
  68.                 Cells(x, 3).Value = Left(stext, iPos - 1)
  69.                 End If
  70.             End If
  71.             iPosL = InStr(stext, " ")
  72.             '****DEBUGGING****'
  73.             If (Len(Left(stext, iPosL - 1)) = 5 And IsNumeric(Left(stext, iPosL - 1))) Then
  74.             '****DEBUGGING****'
  75.                 If IsEmpty(Cells(x, 3).Value) = False Then  'Wenn Inhalt vorhanden
  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, iPosL - 1)   'PLZ einf?gen
  84.                 End If
  85.             End If
  86.             If (y < 4) Then
  87.                 y = y + 1                                       ' Zeile nach LastRow errechnen
  88.                 Cells(x, y).Value = stext                             ' letztes Wort ?bertragen
  89.             End If
  90.         Wend
  91.     End If
  92.                            
  93. '  '* Teilstring am Delimiter auslesen
  94. '    strTeilstring = Split(Trim(wsakt.Cells(x, 2).Value), strTrennzeichen)
  95. '
  96. '  '* Durchlaufen des gesamten Arrays einer Zelle vom ersten bis zum letzten Wert
  97. '  For a = LBound(strTeilstring) To UBound(strTeilstring)
  98. '
  99. '    '* Array-Elemente nacheinander eintragen
  100. '    wsakt.Cells(x, NextCol).Value = Trim(strTeilstring(a))
  101. '
  102. '    '* Zeilenz?hler erh?hen
  103. '    NextCol = NextCol + 1
  104. '  Next a
  105. Next x
  106.  
  107. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement