将标题和范围从一个表格复制到另一个表格
我试图复制一个标题和一组数据到一个新的工作表进行打印。
虽然我可以复制数据罚款列宽度丢失,运行autofit它再次打破了页面。 最初devise页面时,手动设置列宽。
目前我有:
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,但是在使用剪贴板时他们没有任何区别。
我需要做什么来复制表单中的单元格宽度?
格雷厄姆,
复制范围之后,只需获取目标范围的列号,然后遍历源范围中的列,复制.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
您可以遍历源的列,并设置目标范围的相应ColumnWidths:
Dim i As Integer For i = 1 To sourceRange.Columns.Count targetRange.Columns(i).ColumnWidth = sourceRange.Columns(i).ColumnWidth Next