从间接单元格复制格式

时间:2019-01-25 03:49:50

标签: excel vba formatting

我设法使用复制了单元格格式within the same sheet

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

但是在another sheet中,我使用=INDEX(Sheet1!$I$1:$J$255,MATCH(A4,Sheet1!$F$1:$F$255,0),1)Sheet1获取值。

现在如何像复制同一张表格中的单元格一样复制源格式!?

我已经尝试了Google Search中的所有内容,但无济于事!

我什至试图做自己的功能,但仍然没有成功!

'(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

我现在已经快要走了!

请有人这么友善,教我怎么做!

非常感谢!

1 个答案:

答案 0 :(得分:0)

假设要复制的单元格为A1,称为“另一张纸”。 假设您要粘贴到名为“ Sheet1”的工作表的单元格A1中。

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