前两行,我循环将完整的工作表复制到另一个文件..这很好,因为我想要格式化而不是链接回到原始文件,它仅从值中复制。注释掉的第二部分起作用,因为它只复制值..但我希望格式化复制,因为它只在前两行中复制。我怎么能结合这个......我看了其他的例子,我似乎无法得到一个有效的答案。感谢
xlWorksheetSource = xlWorkbookSource.Sheets(sheet.Name)
xlWorksheetSource.Copy(After:=xlWorkbookDestination.Worksheets(xlWorkbookDestination.Worksheets.Count))
'xlWorksheetSource.UsedRange.Copy()
'xlWorkDestSource = xlWorkbookDestination.Worksheets.Add()
'xlWorkDestSource.UsedRange._PasteSpecial(XlPasteType.xlPasteValues, XlPasteSpecialOperation.xlPasteSpecialOperationNone, False, False)
'xlWorkDestSource.Name = sheet.Name
答案 0 :(得分:1)
这将粘贴单元格格式和值:
xlWorksheetSource.UsedRange.Copy()
xlWorkDestSource = xlWorkbookDestination.Worksheets.Add()
xlWorkDestSource.Range("A1").PasteSpecial xlPasteAllUsingSourceTheme
xlWorkDestSource.Range("A1").PasteSpecial xlPasteValues
如果您只需要值和数字格式,则可以执行以下操作:
xlWorksheetSource.UsedRange.Copy()
xlWorkDestSource = xlWorkbookDestination.Worksheets.Add()
xlWorkDestSource.Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
<强>更新强>
要使用合并的单元格,这将有效。 可能有更好/更快的方式
xlWorksheetSource = xlWorkbookSource.Sheets(sheet.Name)
xlWorksheetSource.Copy(After:=xlWorkbookDestination.Worksheets(xlWorkbookDestination.Worksheets.Count))
xlWorkDestSource = xlWorkbookDestination.Worksheets(xlWorkbookDestination.Worksheets.Count)
For each cel in xlWorkDestSource.UsedRange
cel.MergeArea.Value = cel.MergeArea.Value
Next
*请注意,我没有对任何对象进行限定,因为您没有在您的代码中,但它绝对是最佳做法。