从excel中的包装单元格中获取前两行文本
我需要从excel中的封装单元格中获取前两行文本。例如,包装的Excel单元格包含如下文本:
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
我只需要前两个行作为'wrapedtext1wrappedtext2'。是否可能?
I need only the first two lines as 'wrapedtext1wrappedtext2'. Is it possible?
我只需要得到前两行为'wrapedtext1wrappedtext2'。是否可能
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)以像素为单位的行高
1) Row Height in Pixels
2)字体类型和大小
3)行间距
4)单元格合并吗?
5)单元格处于自动调整状态
5) Is the cell in Autofit state
6)是正常模式下的所有文本还是有任何粗体/斜体/ underline字符等等
6) Is all the text in Normal mode or does it have any Bold/Italics/Underline character(s) etc etc
请考虑此快照
例如,像素中的行高可以从
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)。 .Size
但是挑战是在下面的情况下会发生什么?
But the challenge is what would happen in the below scenario?
在我看来,它将是太多的痛苦来实现你想要的。最好的部分是使用 ALT + Enter
来插入换行符,然后检索文本。
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
字符串通过vba代码输入到封装的单元格中。那么如何通过按alt + enter来插入数据? - 1355 4小时前
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
注意:对于上述,您必须记录一个宏,看看可以采用的字体,字体大小和列宽特定格式。同样,你必须考虑这样的事实,我上面给出的例子是一个新的表格中的非格式化单元格。如果您正在写入合并单元格或每格式化单元格,那么您将必须相应地更改上述代码,这可以通过录制宏轻松实现。 我还假设ZOOM级别设置为100%。
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