[英]Copying Format of one excel sheet to another excel worksheet using VBA
Is it possible to copy format of one excel sheet to another worksheet using VBA. 是否可以使用VBA将一个Excel工作表的格式复制到另一个工作表。
Like manually we can do by selecting entire sheet and then click on format button. 像手动一样,我们可以通过选择整个工作表然后单击格式按钮来完成。 And then select other worksheet and format will be copied.
然后选择其他工作表和格式将被复制。 Is it possible to do by code.
是否可以通过代码完成。
Thanks & Regards Sahil Chaudhary 谢谢和问候Sahil Chaudhary
Absolutely. 绝对。 Below is sample code.
以下是示例代码。
see https://msdn.microsoft.com/en-us/library/office/ff837425.aspx 请参阅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
Find below the full code to paste the format of one Worksheet
named "Source" , including Color, ColumnWidth, RowHeight, Comment, DataValidation,
except the contents
( Values, Formulas
) of the cells to all other Worksheets
in the same Workbook
excluding a List of Worksheets
as an Array
在下面找到完整代码,以粘贴一个名为“Source”的
Worksheet
的格式,包括Color, ColumnWidth, RowHeight, Comment, DataValidation,
除了单元格的contents
( Values, Formulas
),除了List of Worksheets
之外的同一Workbook
中的所有其他Worksheets
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.