Hallo mungo11,
Thanks for the example. AutoFit can keep text visible, but it will not keep a row’s exact custom height after the entry moves. Excel tends to move the cell content and most cell formatting. Row height stays with the original row number, so an entry from row 1942 can land in 1947 and pick up 1947’s height.
I think the reliable way is to store each row height in an empty helper column, sort that column with the data, then write the heights back. Please try this on a copy first, and leave the UserForm change as it is.
Use an empty column. The steps below use column L. If L already has data, pick another empty column and use that letter in both the height loop and .SetRange.
Current** Workbook_Open in ThisWorkbook (leave this as the sort **part):
Private Sub Workbook_Open()
With Tabelle1.Sort
.SortFields.Clear
.SortFields.Add Key:=Tabelle1.Range("B5:B2000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
.SetRange Tabelle1.Range("A5:K2000")
.Header = xlNo
.Apply
End With
End Sub
Replace** that whole procedure **with:
Private Sub Workbook_Open()
Dim r As Long
For r = 5 To 2000
Tabelle1.Cells(r, "L").Value = Tabelle1.Rows(r).RowHeight
Next r
With Tabelle1.Sort
.SortFields.Clear
.SortFields.Add Key:=Tabelle1.Range("B5:B2000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
.SetRange Tabelle1.Range("A5:L2000")
.Header = xlNo
.Apply
End With
For r = 5 To 2000
Tabelle1.Rows(r).RowHeight = Tabelle1.Cells(r, "L").Value
Next r
Tabelle1.Range("L5:L2000").ClearContents
End Sub
Current** SortierungderTermine (module **6):
Sub SortierungderTermine()
'
' Makro „SortierungderTermine“
'
' Tastenkombination: Strg+Y
'
Tabelle1.Sort.SortFields.Clear
Tabelle1.Sort.SortFields.Add2 Key:=Tabelle1.Range("B5:B2000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Tabelle1.Sort
.SetRange Tabelle1.Range("A5:K2000")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Replace** that whole procedure **with:
Sub SortierungderTermine()
'
' Makro „SortierungderTermine“
'
' Tastenkombination: Strg+Y
'
Dim r As Long
For r = 5 To 2000
Tabelle1.Cells(r, "L").Value = Tabelle1.Rows(r).RowHeight
Next r
Tabelle1.Sort.SortFields.Clear
Tabelle1.Sort.SortFields.Add2 Key:=Tabelle1.Range("B5:B2000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Tabelle1.Sort
.SetRange Tabelle1.Range("A5:L2000")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For r = 5 To 2000
Tabelle1.Rows(r).RowHeight = Tabelle1.Cells(r, "L").Value
Next r
Tabelle1.Range("L5:L2000").ClearContents
End Sub
The sort keys and header settings are unchanged. The only extra work is column L, and .SetRange now includes L so the stored heights move with the dates.
Then:
- Debug > Compile VBA Project
- Put two dates out of order, with different row heights
- Save, close, and reopen (daily sort)
- Press Ctrl+Y as well
If 1942’s content moves to 1947, 1947 should keep 1942’s height. If Compile flags a line, or a height still doesn’t follow, send the highlighted line and we can check the helper column.