2012-04-17 166 views
2

我一直在尋找這個問題的解決方案一個多星期了。我有一張帶格式文本(顏色和樣式)的表格,其值與第一列的距離不確定。中間有一些空格,但我相信我已經通過使用IsEmpty正確處理了這種情況。我想將每個單元格中的每個值都複製到第二列頂部的一個單元格中。我已經成功地將每個單元格中的文本複製到指定的帶連續符的連接單元格中,但是我一直未能保持格式化。任何人都可以提供關於如何複製格式與本文一起幫助將不勝感激。謝謝!VBA Excel行中的單元格中斷

'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 
+1

請參閱此主題http://www.mrexcel.com/forum/showpost.php?p=3034158&postcount=5安德魯Poulsom已經給出了你想要的代碼。 – 2012-04-17 18:24:27

+2

請注意,該代碼中存在一個錯誤:如果要連接的第一個值具有任何前導空格,則在並置循環後調用'Trim()'會將所有以下格式化步驟的前導空格數修剪掉... – 2012-04-17 18:50:35

+0

感謝您的鏈接,這完成了我正在尋找和更多 – 2012-04-17 18:59:38

回答

0

您可以使用Range.Copy和Range.PasteSpecial性能,易於使用,並解決您的格式細胞的問題。

+0

是的,這將解決複製和粘貼與格式的問題,但是這不會讓我連接到一個單元格的範圍。 – 2012-04-23 15:17:30

相關問題