2

Is it possible to copy format of one excel sheet to another worksheet using VBA.

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

2 Answers 2

6

Absolutely. Below is sample code.

see 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
2
  • Hi , As per your solution we can copy same same data from Source to Target sheet. But my requirement is different. I have to different worksheet having different data. I want to copy format of Source sheet to Target sheet. Not the Data. May 22, 2015 at 6:29
  • Hi Sahil Chaudhardy, I'm afraid my intention was not to provide you with the full code but to give you a sample in order encourage you to research the subject. Now be aware that when the XlPasteType Enumeration refers to formulas unfortunately it also includes values. Nevertheless I'm posting the full code in a separated answer. Cheers...
    – EEM
    May 22, 2015 at 11:23
3

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

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
1
  • Hmmm. I looked at the Xlpastetype list in the link you provided and there doesn't seem to be a way to just paste the borders, without everything else. I see there's all without borders, but I just need borders. Is this possible?
    – niklas24
    Feb 12 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.