|
Hello Forum,
ich habe eine Frage,
ich habe Häufig die Situation das ich Text in einer Zelle quasi als überschrift habe und in der Zeile darunter "normalen" Text.
(bitte nicht Fragen warum Exel dafür das Mittel der Wahl wurde, ich verstehe es auch nicht....)
jetzt wollte icih mir ein Makro schreiben das in der Ausgewählten Zelle die erste Zeile unberühert lässt und ab der 2. Zeile den Text Formatiert auf schwarzu nicht fett,unterstrichen oder Kursiv.
aber Irgendwie will es nicht so wie ich das will....
Es formatiert die 2. Zeile und die erste Zeile bis zur Veränderung des Formates, also bis der Text eine andere Farbe hat oder Kursiv wird o.Ä.
hier ist mein Code (Excel 2015 :\)
Kann mir jemand sagen was hier schief läuft?
' Check if a single cell is selected
If Selection.Cells.Count > 1 Then
MsgBox "Please select a single cell."
Exit Sub
End If
Dim selectedCell As Range
Set selectedCell = Selection.Cells(1, 1)
Dim cellText As String
cellText = selectedCell.Value
' Find the position of the first line break (Chr(10))
Dim firstLineBreakPos As Integer
firstLineBreakPos = InStr(cellText, Chr(10))
' Check if a second line exists
If firstLineBreakPos > 0 Then
' Calculate the start position and length of the second line and subsequent lines
Dim startPos As Integer
startPos = firstLineBreakPos + 1
Dim length As Integer
length = Len(cellText) - firstLineBreakPos
' Change the font color of the characters from the second line onwards to black
selectedCell.Characters(Start:=startPos, length:=length).Font.ColorIndex = xlAutomatic
selectedCell.Characters(Start:=startPos, length:=length).Font.Bold = False
selectedCell.Characters(Start:=startPos, length:=length).Font.Italic = False
selectedCell.Characters(Start:=startPos, length:=length).Font.Underline = xlUnderlineStyleNone
' Optional: Ensure the entire cell's font color is set to automatic/black first if needed
' selectedCell.Font.ColorIndex = xlAutomatic
Else
MsgBox "The selected cell does not contain a second line."
End If
End Sub
|