宏帮助:工作簿到新文档仅复制单元格值

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

我有一个宏,它仅将可见项目从使用名为 (ws1) 的过滤宏的页面复制到名为 (ws2) 的隐藏页面。 单击按钮时,它会取消隐藏 ws2 并复制它,但某些单元格保留公式并引用源工作簿。 我只想使用 xLpastespecial 来复制值,但我似乎无法获得正确的参考范围,或者我没有正确插入它。 下面是我在没有 xLpasetspecial 的情况下使用的内容,它只会让我复制文档,但也复制参考公式。 任何建议都会很棒!

VBA 宏如下:

Sub TestThat()

Dim i As Integer
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Estimated Bill of Materials")
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Copyable ROM")
Dim sourceRange As Range: Set sourceRange = ws1.Range("B2:C7")

sourceRange.Copy
ws2.Visible = xlSheetVisible
ws2.Activate

Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False



ws2.Range("B12:J100").Clear


    For i = 12 To ws1.Range("B100").End(xlUp).Row
        If ws1.Cells(i, 9) = "*" Then ws1.Range(ws1.Cells(i, 1), ws1.Cells(i, 7)).Copy ws2.Rows(ws2.Cells(ws2.Rows.Count, 2).End(xlUp).Row + 1)
    Next i
    ws2.Copy
    ws2.Visible = xlSheetHidden
    
End Sub

尝试将已过滤的工作表复制到新的工作簿中。 只想要值,但得到了公式生成的值。

excel vba
1个回答
0
投票
Option Explicit

Sub TestThat2()

    Dim ws1 As Worksheet, ws2 As Worksheet, rngTo As Range
    Dim r As Long, lastrow As Long

    With ThisWorkbook
        Set ws1 = .Sheets("Estimated Bill of Materials")
        Set ws2 = .Sheets("Copyable ROM")
    End With
    
    ' destination sheet
    With ws2
        .Visible = xlSheetVisible
        .Range("B2:C7").Value2 = ws1.Range("B2:C7").Value2
        .Range("B12:J100").Clear
        Set rngTo = .Cells(.Rows.Count, "B").End(xlUp).Offset(, -1)
    End With
    
    ' source sheet
    With ws1
        lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
        For r = 12 To lastrow
            If .Cells(r, "I") = "*" Then
                ' copy cols A-G to new row
                Set rngTo = rngTo.Offset(1)
                rngTo.Resize(, 7).Value2 = .Cells(r, 1).Resize(, 7).Value2
            End If
        Next
    End With
    ws2.Copy
    ws2.Visible = xlSheetHidden
    
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.