VBA - 将每个工作表保存为excel工作簿中的值

时间:2015-08-17 17:22:46

标签: excel vba excel-vba

我试图编写一个宏来遍历活动工作簿中的每个工作表,并将所有公式保存为值。为此,对于每张纸,首先我遍历每个数据透视表,然后选择表并复制&粘贴为值。然后我尝试使用worksheet.activerange.value = .value方法保存工作表中的其余单元格。

我在行wks.UsedRange.Value = wks.UsedRange.Value上收到1004运行时错误。

我有两个问题:

1)如何修复运行时错误?

2)有没有办法让.value = .value与数据透视表一起使用?在以前的宏中,这从未使用过枢轴,所以我必须使用如下的复制和粘贴。

非常感谢你的帮助!

Sub LockWorkbook()

Dim pvt As PivotTable
Dim wks As Worksheet
Dim i As Integer
Dim n As Integer

n = 1

For Each wks In ActiveWorkbook.Worksheets
    i = 1
    For Each pvt In wks.PivotTables
        wks.PivotTables(i).TableRange2.Select
        With Selection
            .Copy
            .PasteSpecial xlValues
            .PasteSpecial xlFormats
        End With
        i = i + 1
        Next pvt

    Set wks = ActiveWorkbook.Worksheets(n)
    wks.UsedRange.Value = wks.UsedRange.Value
    n = n + 1

    Next wks

End Sub

2 个答案:

答案 0 :(得分:1)

在国王的观点上,您可以简单地将整个工作表复制并粘贴为值。

var socketFactory = module.factory('SocketFactory', function($rootScope){
    var factory = {};
    var instances = {};

    factory.getInstance = function(name, config){
        if(!(name in instances)){
            instances[name] = createNewWebSocketInstance(name, config);
        }
        return instances[name];
    };

    var createNewWebSocketInstance = function(name, config){
        var webSocket = new $window.WebSocket(config.address);
        webSocket.id = uniqid();
        registerWebSocketHandlers(webSocket, name, config.handlers); //etc.
        return webSocket;
    };

    var registerWebSocketHandlers = function(webSocket, name, handlers){
        webSocket.onmessage = function(event){
            $rootScope.$emit('SocketMessageReceived_' + name, event.data);
        };
        //etc...
    };

    return factory;
});

如果你想把它放在一个简单的循环中,可以在所有页面上执行它,它可以看起来像:

var firstConfig = {url: '*****', username: '****', password: '****', etc: '****'};
// You only need to pass in the config the first time.
var firstWebSocket = SocketFactory.getInstance('firstSocket', firstConfig);
var secondConfig = {url: '####', username: '####', password: '####', etc: '####'};
var secondWebSocket = SocketFactory.getInstance('secondSocket', secondConfig);

如果有帮助,请告诉我!

答案 1 :(得分:0)

最新编辑消除了运行时错误“1004”(不包括转换为值的数据透视表)

getString(R.string)