2

我使用 Excel 2010 具有以下功能:

Private Function MakeAllSheetsValuesOnly(targetBookName As String)

If Excel.ActiveWorkbook.Name = Excel.ThisWorkbook.Name Then
Else

        Excel.Workbooks(targetBookName).Activate

        Dim mySheet
        For Each mySheet In Excel.ActiveWorkbook.Sheets
                    With mySheet
                                With .Cells
                                            .Copy
                                            .PasteSpecial Excel.xlPasteValues
                                End With
                                .Select
                                .Range("A1").Select
                    End With
                    Excel.ActiveWindow.SmallScroll Down:=-200
                    Excel.Application.CutCopyMode = False
        Next mySheet
End If

End Function                        'MakeAllSheetsValuesOnly

它有效,但我宁愿不依赖剪贴板是否有另一种方法可以只制作所有工作表值?

刚刚找到了我在另一个与该主题相关的程序中使用的替代逻辑:

Dim rSource As Range
Dim rDest As Range

Set rSource = .Range("C5:BG" & .Range("B4").Value + 4)   
Set rDest = mySummaryBook.Sheets("Data_Measures").Cells(Rows.Count, 4).End(xlUp)(2, 1)
    With rSource
          Set rDest = rDest.Resize(.Rows.Count, .Columns.Count)
    End With
    rDest.Value = rSource.Value
Set rSource = Nothing
Set rDest = Nothing
4

3 回答 3

11

也许是这样的:

With mySheet.UsedRange
    .Value = .Value
End With
于 2012-06-19T17:08:36.703 回答
1

基于上面蒂姆的回答,这似乎是最有效的方法,您可以清理代码以使其更快,更少的资源密集型。见下文。没有太大的变化,但将有助于处理。首先不需要函数。子会做。无需选择和激活这么多东西:

Private Sub MakeAllSheetsValuesOnly(targetBookName As String)

If ActiveWorkbook.Name <> ThisWorkbook.Name Then

        Dim wkb As Workbook
        Set wkb = Workbooks(targetBookName)

        With wkb
            Dim mySheet As Worksheet
            For Each mySheet In wkb.Worksheets
                mySheet.UsedRange.Value = mySheet.UsedRange.Value
            Next mySheet
        End With
End If

End Sub 'MakeAllSheetsValuesOnly
于 2012-06-19T17:08:12.633 回答
1

您不需要为此提供功能。

蒂姆已经给了你一个很好的方法。这是另一种方式...

Sub Sample()
    MakeAllSheetsValuesOnly "Book2"
End Sub

Private Sub MakeAllSheetsValuesOnly(targetBookName As String)
    Dim mySheet As Worksheet
    Dim formulaCell As Range
    Dim aCell As Range

    Application.ScreenUpdating = False

    For Each mySheet In Workbooks(targetBookName).Sheets
        On Error Resume Next
        Set formulaCell = mySheet.Cells.SpecialCells(xlCellTypeFormulas)
        On Error GoTo 0

        If Not formulaCell Is Nothing Then
            For Each aCell In formulaCell
                aCell.Value = aCell.Value
            Next
        End If
    Next mySheet

    Application.ScreenUpdating = True
End Sub
于 2012-06-19T17:15:42.547 回答