Paste Values instead of formulas with PasteSpecial - VBANewbie

后端 未结 3 1433
悲&欢浪女
悲&欢浪女 2021-01-25 10:33

I am absolutely new to vba. I want to copy certain values in cells from two tabs (\"Equities\", \"Bonds\") into a third one (\"ZSM\") with the following code.

Su         


        
相关标签:
3条回答
  • 2021-01-25 11:14

    You can try replacing those Activesheet.PasteSpecial as:

    Selection.PasteSpecial Paste:=xlPasteValues
    

    This will paste your selected range as just values.

    0 讨论(0)
  • 2021-01-25 11:18

    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

    0 讨论(0)
  • 2021-01-25 11:22

    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.

    0 讨论(0)
提交回复
热议问题