简体   繁体   English

VBA Excel单元格中的换行符

[英]VBA Excel Line Break in Cell

I've been searching for a solution to this problem for over a week now. 我一直在寻找解决此问题的解决方案已有一个多星期了。 I have a sheet with formatted text (colors and styling) with values an undetermined distance down the first column. 我有一张带有格式化文本(颜色和样式)的工作表,其值沿第一列的距离不确定。 There are some spaces in between but I believe I've handled that situation correctly by using IsEmpty. 两者之间有一些间隔,但是我相信我已经通过使用IsEmpty正确地处理了这种情况。 I want to copy each of these values from each cell all to one cell at the top of the second column. 我想将所有这些值从每个单元格全部复制到第二列顶部的一个单元格中。 I've been successful in being able to copy the text from each of these cells into a specified concatenated cell with line breaks, however I've been unsuccessful at keeping the formatting. 我已经成功地将每个单元格中的文本复制到带换行符的指定级联单元格中,但是在保持格式方面却一直没有成功。 Any help anyone can provide on how to copy the formatting along with this text would be greatly appreciated. 任何人都可以提供有关如何复制格式以及此文本的任何帮助,将不胜感激。 Thank you! 谢谢!

'set variable to find the last row of the sheet
Dim LastRow As Integer
'find the last row of the active sheet
LastRow = ActiveSheet.UsedRange.Rows.Count
'loop through each of the rows
For C = 1 To LastRow
    'determine if a cell has a value in it - if so complete below commands
    If (IsEmpty(Cells(C, 1).Value) = False) Then
    'leave the contents of the first cell in the second column, insert a linebreak and copy the values from the current cell in the first column
    Cells(1, 2).Value = Cells(1, 2).Value & Chr(10) & Cells(C, 1).Value
    End If
Next C

您可以使用Range.Copy和Range.PasteSpecial属性,这些属性易于使用并解决了格式化单元格的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM