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 但在使用剪贴板时它们没有区别。

我需要做什么才能跨工作表复制单元格宽度?

4

2 回答 2

3

您可以遍历源的列,并设置目标范围的相应 ColumnWidths:

Dim i As Integer
For i = 1 To sourceRange.Columns.Count
    targetRange.Columns(i).ColumnWidth = sourceRange.Columns(i).ColumnWidth
Next
于 2009-07-03T14:16:53.340 回答
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
于 2009-07-03T14:41:17.823 回答