2009-07-03 116 views
0

我試圖將一個表頭和一組數據複製到新的工作表進行打印。將表單和範圍從一個表單複製到另一個表單

雖然我可以複製數據罰款列寬度丟失,並在其上運行autofit再次打破了頁面。列的寬度是在頁面最初設計時手動設置的。

目前我有:

Dim tmp As Worksheet 
Set tmp = Sheets.Add(after:=ActiveSheet) 
RD.Copy tmp.Range("A1") ' Range data (set elsewhere) 
RH.Copy tmp.Range("A1") ' Range header (set elsewhere) 

我使用xlPasteSpecial和xlPasteAll嘗試,但他們給沒有區別,而使用剪貼板。

我需要做什麼來複製紙張寬度單元格?

回答

3

格雷厄姆,

整個複製範圍後,剛剛獲得的列數目標範圍,然後通過源範圍複製的.ColumnWidth資源在列迭代...

Dim RD As Range 
Dim RH As Range 

Set RH = Cells.Range("A1:C1") 
Set RD = Cells.Range("A2:C2") 

Dim tmp As Worksheet 
Set tmp = Sheets.Add(after:=ActiveSheet) 
RD.Copy tmp.Range("A1") ' Range data (set elsewhere)' 
RH.Copy tmp.Range("A2") ' Range header (set elsewhere)' 

' Get the column number of the first cell in the destination range' 
' because it looks like we are just setting the first cell of the destination range' 
' and letting Excel roll the range over' 
Dim tmpCol As Integer 
tmpCol = tmp.Range("A1").Cells(1, 1).Column ' make sure you use the same range value' 

Now loop through the source columns setting the dest column to the same width. 
For Each objCol In RH.Columns 

    tmp.Columns(tmpCol).ColumnWidth = objCol.ColumnWidth 
    tmpCol = tmpCol + 1 

Next 
+0

雖然不是第一個,但這是更詳細的迴應。 – 2009-07-03 15:20:49

3

你可以通過源的列循環,並設定目標範圍的相應的列寬:

Dim i As Integer 
For i = 1 To sourceRange.Columns.Count 
    targetRange.Columns(i).ColumnWidth = sourceRange.Columns(i).ColumnWidth 
Next 
+0

這會工作。我被複制的心態固定。 – 2009-07-03 14:30:39

相關問題