I need to take the first two lines of text from a wrapped cell in Excel. For example, a wrapped Excel cell contains the text as follows:
wrapedtext1
wrappedtext2
wrappedtext3
wrappedtext4
I need only the first two lines as 'wrapedtext1wrappedtext2'. Is it possible?
In the Formulas Helper dialog, please do as follows: Select Text from the Formula type drop-down list; Select Extract the nth word in cell in the Choose a formula list box; In the Cell textbox, specify the cells that you will extract words from, and enter the number you want to extract word based on in The Nth textbox.
To constrain your text so only a certain amount of it appears regardless of how much a cell contains, you can double-click on the cell, insert your cursor after the last character you want to be visible and press "Alt-Enter" to add a manual line break.
I only need to get first two lines as 'wrapedtext1wrappedtext2' .Is it possible???
Yes it might be possible but there is NO SIMPLE way to achieve it. There are lot of factors that you will have to consider.
1) Row Height in Pixels
2) Font Type and Size
3) Line Spacing
4) Is the Cell Merged?
5) Is the cell in Autofit state
6) Is all the text in Normal mode or does it have any Bold/Italics/Underline character(s) etc etc
Consider this snapshot
For example, Row Height in Pixels can be derived from
Debug.Print Range("A1").Height * (24 / 18)
Font Size in the above case can be achieved from this
Debug.Print Range("A1").Font.Size
But the challenge is what would happen in the below scenario?
In my opinion, it would be too much of a pain to achieve what you want. The best part would be to use ALT + Enter
to insert line breaks and then retrieve the text.
FOLLOWUP
The strings are entered into the wrapped cell through vba code. So How will insert data by pressing alt + enter? – 1355 4 hours ago
In such a scenario, you can take a similar approach as well.
Sub Sample()
Dim strg As String
strg = "This is a sample" & vbCrLf & _
"sentence which is" & vbCrLf & _
"in Cell A1 and the" & vbCrLf & _
"text is separated" & vbCrLf & _
"with line breaks"
With Range("A1")
.Columns(1).ColumnWidth = 16.86
.Font.Name = "Calibri"
.Font.Size = 11
.Value = strg
End With
End Sub
NOTE: For the above you will have to record a macro and see what is the font, font size and column width that can take a particular formatting. Again, you will have to consider the fact that the example that I have given above is for a non formatted cell in a new sheet. If you are writing to a merged cell or a per-formatted cell then you will have to change the above code accordingly which can be easily achieved by recording a macro. I am also assuming that the ZOOM level is set to 100%
SNAPSHOTS
HTH
Sid
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With