How to Copy Cells on Excel With All Atribute With VBA Function as Excel Formula

Viewed 33

I want to copy 1 cell on Excel with attributes (color, background color, border) to Active Cell with Excel Formula. So, I can write

=CopyCell(B2)

and the Active Cell copying B2 with all atributes. Can you help me?

I just succeed to copy one cell to another using button onClick with calling this procedure

Public Function CopyOnlyFormatConditionColor(ByVal Source As String, ByVal Destination As String)
    Worksheets("Sheet2").Range(Source).Copy Worksheets("Sheet1").Range(Destination)
End Function

Thanks to any help

1 Answers

I have not solved this; but this is what I have so far. The stack overflow answering guidelines ask to share what I have discovered, in case it helps the next person...


    Public Function CopyColor(Target As Range)
    
    Dim iCol%, iCol2%
    Dim iRow&, iRow2&
    Dim sSheet$, sSheet2$
    Dim WS As Worksheet, WS2 As Worksheet
    
    Set WS = Target.Worksheet
    sSheet = WS.Name
    iRow = Target.Row
    iCol = Target.Column
    
    Set WS2 = Application.ActiveCell.Worksheet
    sSheet2 = WS2.Name
    iRow2 = Application.ActiveCell.Row
    iCol2 = Application.ActiveCell.Column
    
    WS.Cells(iRow, iCol).Copy
    WS2.Cells(iRow2, iCol2).PasteSpecial (xlPasteValues)
    WS2.Cells(iRow2, iCol2).PasteSpecial xlPasteFormats
    Application.CutCopyMode = False
    
    End Function

Related