Optimize this?

Jokada

New Member
Joined
Dec 6, 2005
Messages
12
Hi,

I'd like to remove all the linked values in a file and replace them by the actual values.

I've created this code:

Code:
 For j = 1 To tWBo.Worksheets.Count
                    Sheets(j).Select
                    Cells.Select
                    Selection.Copy
                    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Next j

More efficient would be if I can make a dynamic array with the number of sheets.
I.e:

Code:
Sheets(Array(1,2,3,4,5)).copy

Is there a way to create an array with size (Worksheets.Count - 1)
And then fill with 1, 2, 3, ... till (Worksheets.Count - 1)?

Any help would be greatly appreciated!!
 

Some videos you may like

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

BrianB

Well-known Member
Joined
Feb 17, 2003
Messages
8,127
I think your basic method is OK, however I have not had much use for Copy/PasteSpecial like this - so could be wrong.

You might like to test these 2 versions and let us know which is best.
Version 2 assumes that the ActiveSheet.UsedRange is the same or larger than in the other sheets. You could make it ActiveSheet.Cells.Copy but this would add to the processing time.
Code:
'-------------------------------------------------------
'- loop through sheets
Sub test1()
    Dim ws As Worksheet
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    For Each ws In ActiveWorkbook.Worksheets
        ws.UsedRange.Copy
        ws.UsedRange.PasteSpecial Paste:=xlValues, _
            Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    Next
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = True
    MsgBox ("Done")
End Sub
'------------------------------------------------------------------
'- select all sheets
Sub test2()
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Worksheets.Select
    ActiveSheet.UsedRange.Copy
    ActiveSheet.UsedRange.PasteSpecial Paste:=xlValues, _
        Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = True
    MsgBox ("Done")
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,122,364
Messages
5,595,718
Members
414,013
Latest member
tnobbs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top