简体   繁体   中英

Copy formats from an indirect cell

I managed to copy cell formats within the same sheet by using

Dim c As Boolean

Private Sub Worksheet_Change(ByVal Target As Range)
    If c Then Exit Sub
    c = True
    f = Mid(Target.Formula, 2)
    Range(f).Copy
    Target.PasteSpecial xlPasteAllUsingSourceTheme
    c = False
End Sub

But in another sheet , I use =INDEX(Sheet1!$I$1:$J$255,MATCH(A4,Sheet1!$F$1:$F$255,0),1) to get the value from Sheet1 .

Now how can I copy the source format just like the cells in the same sheet would!?

I had tried everything in Google Search to no avail!

I even tried to make my own function and still failing to do so!!!

'(One variation)
Function CopyFrom(ByVal Cell)

    Dim r As Range

    Set r = Worksheets(Cell.Parent.Name).Range(Cell.address(External:=False))
    CopyFrom = r.Value2
    r.Copy
    Application.Caller.PasteSpecial xlPasteAllUsingSourceTheme

End Function

I'm kind of at the end of my rope now!

Somebody PLEASE be so kind and teach me how to do it!

Much appreciated!!!

Assuming the cell you want to copy is A1 on sheet called "another sheet". Assuming you want to paste in cell A1 on sheet called "Sheet1".

option explicit
dim wb as workbook
dim wsSource as worksheet, wsDest as worksheet
dim Target As Range


Sub test()
set wb = thisworkbook
set wsSource = wb.Sheets("another sheet")
set wsDest = wb.Sheets("Sheet1")
Set Target = wsDest.Range("A1")

    wsSource.Range("A1").Copy
    Target.PasteSpecial xlPasteAllUsingSourceTheme
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM