使用VBA将一个Excel工作表的格式复制到另一个Excel工作表

时间:2015-05-21 03:33:11

标签: vba excel-vba excel-formula excel

是否可以使用VBA将一个Excel工作表的格式复制到另一个工作表。

与手动一样,我们可以通过选择整张表格然后点击格式按钮来完成。然后选择其他工作表和格式将被复制。可以通过代码来完成。

谢谢&问候 Sahil Chaudhary

2 个答案:

答案 0 :(得分:5)

绝对。以下是示例代码。

请参阅https://msdn.microsoft.com/en-us/library/office/ff837425.aspx

Sub Wsh_PasteSpecial()
Dim WshSrc As Worksheet
Dim WshTrg As Worksheet

Rem Set working worksheets
Set WshSrc = ThisWorkbook.Worksheets("Source")
Set WshTrg = ThisWorkbook.Worksheets("Target")

    WshSrc.Cells.Copy
    With WshTrg.Cells
        .PasteSpecial Paste:=xlPasteColumnWidths
        .PasteSpecial Paste:=xlPasteFormats
        .PasteSpecial Paste:=xlPasteFormulasAndNumberFormats
        Application.CutCopyMode = False
    End With    
End Sub

答案 1 :(得分:3)

在下面找到完整代码,以粘贴一个名为“来源”Worksheet格式,包括除Color, ColumnWidth, RowHeight, Comment, DataValidation,以外的contents Values, Formulas Worksheets )相同Workbook中的所有其他List of Worksheets的单元格除了Array Option Explicit Sub Wsh_PasteSpecial_Test() Dim aWshExcluded As Variant, vWshExc As Variant aWshExcluded = Array("Exclude(1)", "Exclude(2)") Dim WshSrc As Worksheet Dim WshTrg As Worksheet Rem Set Source Worksheet Set WshSrc = ThisWorkbook.Worksheets("Source") Application.ScreenUpdating = 0 Rem Process All Worksheets For Each WshTrg In WshSrc.Parent.Worksheets Rem Exclude Worksheet Source If WshTrg.Name <> WshSrc.Name Then Rem Validate Worksheet vs Exclusion List For Each vWshExc In aWshExcluded If WshTrg.Name = vWshExc Then GoTo NEXT_WshTrg Next Rem Process Worksheet Target With WshTrg.Cells WshSrc.Cells.Copy .PasteSpecial Paste:=xlPasteFormats 'Source format is pasted. .PasteSpecial Paste:=xlPasteComments 'Comments are pasted. .PasteSpecial Paste:=xlPasteValidation 'Validations are pasted. Application.CutCopyMode = False Application.Goto .Cells(1), 1 End With: End If: NEXT_WshTrg: Next Application.Goto WshSrc.Cells(1), 1 Application.ScreenUpdating = 1 End Sub

SUBSTR( MAX(CONCAT(0+column, column)), 2)