I want to write a VBA script that takes contents from a .txt file and splits the data into an Excel table. My script works as intended, but I am still struggling with the format.
The data for each cell is a list with items, and sometimes these items have multiple sublists. I want the list to be aligned to the left, and for each sublist, I want it to be indented with a tab within the text.
The result in Excel from my script only takes the value of the cell and doesn’t apply the formatting. However, if I double-click on a specific cell, the text appears in the intended format and stays this way if I unselect the cell. How can I rewrite my script to ensure proper formatting?
I tried worksheet.calculate, worksheet.Rows.AutoFit
' Arbeitsblatt festlegen
Set wb = Workbooks.Add
Set ws = wb.ActiveSheet
ws.Cells.VerticalAlignment = xlTop
ws.Columns(1).ColumnWidth = 20.25 ' Setzt Spalte 1 auf ca. 200 Pixel
ws.Columns(2).ColumnWidth = 103.58 ' Setzt Spalte 2 auf ca. 1250 Pixel
Then there is some code that iterates through the txt data an cleans it up.
The value setting takes place in this for loop with
For j = 0 To UBound(ZeilenArray) - 1
If InStr(1, ZeilenArray(j), SplitArray(nest) & ")") = 1 Then
currentRow = currentRow + 1
ws.Cells(currentRow, 1).Value = currentName & " " & SplitArray(nest)
nest = nest + 1
ws.Cells(currentRow, 2).Value = Replace(ZeilenArray(j), "@", "")
Else
ws.Cells(currentRow, 2).Value = ws.Cells(currentRow, 2).Value & Replace(ZeilenArray(j), "@", "")
End If
Next j
and at the end
' Berechnung forcieren und Anzeige aktualisieren
Set Rng = ws.UsedRange
' Durchlaufe jede Zelle im Bereich
For Each cell In Rng
cell.Value = cell.Value ' Setzt den Wert der Zelle, um die Formatierung anzuwenden
DoEvents ' Erlaubt Excel, die Benutzeroberfläche zu aktualisieren
Next cell
rrss25 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1