问题
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?
回答1:
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
回答2:
Use the FIND function:
=LEFT(B2,(FIND(CHAR(10),B2)))
- FIND searches for the line break character (CHAR(10)
- LEFT gives you the first X characters in B2, up to the number where it finds the line break
回答3:
Hi ok there are two ways how to do it with the VBA Code.
First way. If the Lines are Separated with a space
Dim avarSplit As Variant
'If separated with a space
avarSplit = Split(Worksheets(2).Range("A7").Value, " ")
second way if they are separated with a break.
Dim avarSplit2 As Variant
'separated with a break
avarSplit2 = Split(Worksheets(2).Range("A8").Value, Chr(10))
Then you get an array where all the lines are seperated and you can just read them out...
Moosli
回答4:
You can do some complicated formula in Excel to achieve this without VBA code.
Assuming the multi-line text is in A1. If you just want the first two lines, you can do this:
Get the first line:
=MID(A1,1,IF(2=LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(10),""))+1,LEN(A1),SEARCH("@",SUBSTITUTE(A1,CHAR(10),"@",2))))
Get the second line:
=MID(A1,SEARCH("@",SUBSTITUTE(A1,CHAR(10),"@",1))+1,IF(2=LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(10),""))+1,LEN(A1),SEARCH("@",SUBSTITUTE(A1,CHAR(10),"@",2)))-SEARCH("@",SUBSTITUTE(A1,CHAR(10),"@",1)))
Then simply use CONCATENATE()
to combine those two.
Have a look at this site that allow you to specify which line to get: http://www.excelblog.ca/separating-lines-from-multi-line-excel-cell/
来源:https://stackoverflow.com/questions/9889002/how-to-get-first-two-lines-of-text-from-a-wraped-cell