粘贴为值

问题描述 投票:0回答:1

我真的需要帮助将以下数据集粘贴为值。

我目前想要遍历工作簿中的某些工作表,并复制和粘贴所选工作表中的数据。

但是,我不确定如何粘贴我正在处理的数据作为值。

我编写了以下代码来遍历我想要的工作表,并从每个工作表中选择我想要的日期。但是,当我将此数据粘贴到主数据选项卡时,我似乎无法粘贴为值。

    Sub exa()
Dim wks As Worksheet

For Each wks In ThisWorkbook.Worksheets
If Not wks.Name = "Master Data" _
And Not wks.Name = "Query --->" _
And Not wks.Name = "Pivot Portfolio Movement" _
And Not wks.Name = "PortfolioMovement - All" _
And Not wks.Name = "Bank Holidays" _
And Not wks.Name = "Property" _
And Not wks.Name = "Postcodes" _
And Not wks.Name = "Product" _
And Not wks.Name = "PartRedemption" _
And Not wks.Name = "Wrap" _
And Not wks.Name = "Completions Database" _
And Not wks.Name = "Default" _
And Not wks.Name = "ReturningBorrower" _
And Not wks.Name = "Extensions" _
And Not wks.Name = "PortfolioMovement" _
And Not wks.Name = "Drawdowns" _
And Not wks.Name = "Dev Interest WIP" _
And Not wks.Name = "Write Off Loans" _
And Not wks.Name = "Interest Rate" _
And Not wks.Name = "Admin" _
And Not wks.Name = "Datatape --->" _
And Not wks.Name = "Data" _
And Not wks.Name = "Drawn Balance by Loan" _
And Not wks.Name = "Sheet1" Then
wks.Range("H2:N2" & wks.Cells(Rows.Count, "N").End(xlUp).Row).Copy _
Destination:=Worksheets("Master Data").Cells(Rows.Count, "A").End(xlUp).Offset(1)
End If
Next
End Sub
excel vba copy-paste
1个回答
0
投票

要粘贴为值,您需要使用Range.PasteSpecial method.PasteSpecial xlPasteValues

请注意,如果您使用数组来跳过工作表,则可以减少以下代码:

Option Explicit

Public Sub exa()
    Dim SkipWorksheets As Variant
    SkipWorksheets = Array("Master Data", "Query --->", "Pivot Portfolio Movement", "PortfolioMovement - All", _
                           "Bank Holidays", "Property", "Postcodes", "Product", "PartRedemption", "Wrap", _
                           "Completions Database", "Default", "ReturningBorrower", "Extensions", _
                           "PortfolioMovement", "Drawdowns", "Dev Interest WIP", "Write Off Loans", _
                           "Interest Rate", "Admin", "Datatape --->", "Data", "Drawn Balance by Loan", "Sheet1")


    Dim Wks As Worksheet
    For Each Wks In ThisWorkbook.Worksheets
        If Not (UBound(Filter(SkipWorksheets, Wks.Name)) > -1) Then
            Wks.Range("H2:N2" & Wks.Cells(Rows.Count, "N").End(xlUp).Row).Copy
            Worksheets("Master Data").Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End If
    Next Wks
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.