Profi-Excel.de

Die Seite für den professionellen Umgang mit Excel und VBA

Excel Tabelle in Textform konvertieren

27. Oktober 2006

Der markierte Bereich wird spaltenweise auf die Textlänge untersucht und damit die Breite der Spalten (Anzahl der Buchstaben) festgelegt. Dabei wird die Text-Eigenschaft des Range-Objektes verwendet, so dass der angezeigte Text erscheint.

Das Makro ist besonders geeignet, um Tabellenausschnitte in der Microsoft Excel Newsgroup einzufügen.

Das Ergebnis sieht dann so aus:

VBA:
  1. +--------+-----------+----------+
  2. |Dieses  |Makro      |erstellt  |
  3. +--------+-----------+----------+
  4. |perfekt |formatierte|ASCII     |
  5. +--------+-----------+----------+
  6. |Tabellen|per        |Knopfdruck|
  7. +--------+-----------+----------+

Makro als Add-In kostenlos herunterladen (inkl. Menüleiste): Profi-ExcelAdd-In

VBA:
  1. Sub exportAscii()
  2. 'konvertiert den markierten Tabellenbereich in
  3. 'eine Tabelle im Textformat
  4. '10-2006
  5. '©E.Bimczok
  6. 'http://www.profi-excel.de/
  7. Const mypath As String = "c:\tempAsciiTable.txt"
  8.  
  9. Dim myRng As Range
  10. Dim myCol As Range
  11. Dim myCell As Range
  12. Dim myLen  As Long
  13. Dim maxLen As Long
  14. Dim numRows As Long
  15. Dim numCols As Long
  16. Dim AsciiOut() As String
  17. Dim AsciiCount As Long
  18. Dim fF As Long
  19. Dim noOfSpaces As Long
  20. Dim spaceStr As String
  21.  
  22. Set myRng = Selection
  23. numRows = myRng.Rows.Count
  24. numCols = myRng.Columns.Count
  25. ReDim AsciiOut(1 To numRows * 2 + 1)
  26.  
  27. For Each myCol In myRng.Columns
  28. maxLen = 0
  29. For Each myCell In myCol.Rows
  30. myLen = Len(myCell.Text)
  31. maxLen = maxAB(maxLen, myLen)
  32. Next myCell
  33.  
  34. AsciiCount = 1
  35. For Each myCell In myCol.Rows
  36. AsciiOut(AsciiCount) = AsciiOut(AsciiCount) & _
  37. "+" & String(maxLen, "-")
  38. AsciiCount = AsciiCount + 1
  39. AsciiOut(AsciiCount) = AsciiOut(AsciiCount) & _
  40. "|" & myCell.Text
  41. noOfSpaces = maxLen - Len(myCell.Text)
  42. spaceStr = ""
  43. If noOfSpaces> 0 Then spaceStr = String(noOfSpaces, " ")
  44. AsciiOut(AsciiCount) = AsciiOut(AsciiCount) & spaceStr
  45. AsciiCount = AsciiCount + 1
  46. Next myCell
  47. AsciiOut(numRows * 2 + 1) = AsciiOut(1)
  48. Next myCol
  49.  
  50. For k = 1 To numRows * 2 + 1
  51. AsciiOut(k) = AsciiOut(k) & Left(AsciiOut(k), 1)
  52. Next k
  53.  
  54. fF = FreeFile
  55. Open mypath For Output As #fF
  56. For k = 1 To UBound(AsciiOut)
  57. Print #fF, AsciiOut(k)
  58. Next k
  59. Close #fF
  60.  
  61. Shell "notepad.exe " & mypath, vbNormalFocus
  62. End Sub
  63.  
  64. Function maxAB(A As Long, B As Long) As Long
  65. If A>= B Then
  66. maxAB = A
  67. Else
  68. maxAB = B
  69. End If
  70. End Function

Kommentar schreiben

XHTML: Sie können diese Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>