Removing first line in multiline Cell

Viewed 165

lets say i have in cell A1

Row A
1 line1
line2
line3

How would i remove just the first line so its end up:

Row A
1 line2
line3
Sub line_count()
Set c = Worksheets("Sheet1")
Dim len_act As Integer
len_act = Len(c.Cells(4, 2)) - Len(Application.WorksheetFunction.Substitute(c.Cells(4, 2), Chr(10), vbNullString))
If len_act > 2 Then
    c.Cells(4, 2) = remove first line
End If
End Sub
2 Answers

You can using this function to remove first line:

Function RemoveFirstLine(oCell As Object)
        Dim strVal As String
        Dim arr() As String
        Dim i As Integer
        Dim strNewVal As String
        
        strVal = oCell.Value
        arr = Split(strVal, vbLf)
        For i = 1 To UBound(arr)
            If i > 1 Then
              strNewVal = strNewVal & vbLf
            End If
            strNewVal = strNewVal & arr(i)
        Next
        oCell.Value = strNewVal
    End Function

call it your code:

 Call RemoveFirstLine(c.Cells(4, 2))
Sub remove_line()
    
  Dim rng As Range: Set rng = Sheets("Sheet1").Range("A1")
  
  rng.Select
  Dim second_line As String: second_line = Split(Selection.Value, vbLf)(1)
  Dim third_line As String: third_line = Split(Selection.Value, vbLf)(2)

  rng = second_line & Chr(10) & third_line
    
End Sub
Related