1

I work with MIT Excel tables. I need to copy "cell fill" to "another fill" and I've done this by using this code:

Sub Macro1()
    Columns("A:A").Copy
    Columns("B:B").Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Range("B1").Select
    Application.CutCopyMode = False
End Sub

How can I modify it so that it works between two different sheets?

Annija
  • 11
  • 1

1 Answers1

0

For that you have to avoid using .Select and start working with objects. You may want to see THIS

So your code can be written as

Sub Sample()
    Dim ws1 As Worksheet, ws2 As Worksheet

    '~~> Change as Applicable
    Set ws1 = ThisWorkbook.Sheets("Sheet1")
    Set ws2 = ThisWorkbook.Sheets("Sheet2")

    ws1.Columns("A:A").Copy
    ws2.Columns("B:B").PasteSpecial Paste:=xlPasteFormats, _
                                    Operation:=xlNone, _
                                    SkipBlanks:=False, _
                                    Transpose:=False
    Application.CutCopyMode = False
End Sub

EDIT

Further to the discussion in comments, if you want to use the Codenames, then use this

Sub Sample()
    Dim ws1 As Worksheet, ws2 As Worksheet

    '~~> Change as Applicable
    Set ws1 = Sheet1
    Set ws2 = Sheet2

    ws1.Columns("A:A").Copy
    ws2.Columns("B:B").PasteSpecial Paste:=xlPasteFormats, _
                                    Operation:=xlNone, _
                                    SkipBlanks:=False, _
                                    Transpose:=False
    Application.CutCopyMode = False
End Sub
Community
  • 1
  • 1
Siddharth Rout
  • 142,730
  • 17
  • 199
  • 246