2009-07-03 92 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

相关问题