Paste Values instead of formulas with PasteSpecial - VBANewbie

左心房为你撑大大i 提交于 2019-12-02 05:24:19

Using the direct value transfer methods described in your last question, I've come up with this.

Each part of the transfer is labelled so you can split the individual routines apart as needed.

Option Explicit

Sub AllesAufEinmal()

    Dim tws As Worksheet

    Set tws = Worksheets("ZSM")

    Call Spalten(tws)
    'Call Wertpapiere(tws)
    'Call Daten(tws)

End Sub

Sub Spalten(zsm As Worksheet)
' Spalten Macro

    'headers, ISIN and data from from Equities
    With Worksheets("Equities")
        With .Range(.Cells(.Rows.Count, "A").End(xlUp), .Cells(4, .Columns.Count).End(xlToLeft))
            zsm.Cells(4, "A").Resize(.Rows.Count, .Columns.Count) = .Value
        End With
    End With

    'headers from Bonds
    With Worksheets("Bonds")
        With .Range(.Cells(4, "B"), .Cells(4, .Columns.Count).End(xlToLeft))
            zsm.Cells(4, zsm.Columns.Count).End(xlToLeft).Offset(0, 1).Resize(.Rows.Count, .Columns.Count) = .Value
        End With
    End With

    'ISIN from Bonds
    With Worksheets("Bonds")
        With .Range(.Cells(5, "A"), .Cells(.Rows.Count, "A").End(xlUp))
            zsm.Cells(zsm.Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(.Rows.Count, .Columns.Count) = .Value
        End With
    End With

    'data from Bonds
    With Worksheets("Bonds")
        With .Range(.Cells(.Rows.Count, "B").End(xlUp), .Cells(5, .Columns.Count).End(xlToLeft))
            zsm.Cells(zsm.Cells(zsm.Rows.Count, "B").End(xlUp).Row, _
                      zsm.Cells(5, zsm.Columns.Count).End(xlToLeft).Column). _
                Offset(1, 1).Resize(.Rows.Count, .Columns.Count) = .Value
        End With
    End With

End Sub

'Best practice' dictates that you should avoid Select and provide proper parent worksheet references. To this end, I've passed the target worksheet reference to each 'helper' sub procedure as a parameter.

You should use xlPasteValues. Example:

Range("B5").PasteSpecial xlPasteValues

If you prefer formulas you could use xlPasteFormulas.

I strongly advise to read this article on how to avoid using Select:

How to avoid using Select in Excel VBA

You can try replacing those Activesheet.PasteSpecial as:

Selection.PasteSpecial Paste:=xlPasteValues

This will paste your selected range as just values.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!