Array VBA issue

WindsorKnot

Board Regular
Joined
Jan 4, 2009
Messages
160
Hi,

I'm trying to make my code run faster by using a dynamic Array.

However, I keep on getting an error everytime I run my code. The error is Object variableor with block variable not set.

Where the error occurs is highlighted in red.

Can someone help me out with this particular issue?

Thanks.

Code:
Sub rangevalue()

    Application.ScreenUpdating = False
    Dim lsheets() As Worksheet, i As Integer, NSheets as Integer
 
    Nsheets = ActiveWorkbook.Worksheets.Count
    ReDim lsheets(1 To Nsheets) ' Declares the array variable size

    For i = 1 To Nsheets
        [COLOR=red][B]lsheets(i).Activate[/B][/COLOR]
        Cells.Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues
    Next i
    Application.CutCopyMode = False
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You haven't actually assigned anything to the array, and to be honest I don't see what purpose it serves here. Try:
Code:
Sub rangevalue()

    Application.ScreenUpdating = False
    Dim i As Integer, NSheets as Integer
 
    Nsheets = ActiveWorkbook.Worksheets.Count
    For i = 1 To Nsheets
        with sheets(i).usedrange
            .copy
            .PasteSpecial Paste:=xlPasteValues
        end with
    Next i
    Application.CutCopyMode = False
End Sub
 
Upvote 0
try something like this...

Code:
Sub rangevalue()
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        ws.Cells.Copy
        ws.Cells.PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
    Next ws
End Sub
 
Upvote 0
You haven't actually assigned anything to the array, and to be honest I don't see what purpose it serves here. Try:
Code:
Sub rangevalue()
 
    Application.ScreenUpdating = False
    Dim i As Integer, NSheets as Integer
 
    Nsheets = ActiveWorkbook.Worksheets.Count
    For i = 1 To Nsheets
        with sheets(i).usedrange
            .copy
            .PasteSpecial Paste:=xlPasteValues
        end with
    Next i
    Application.CutCopyMode = False
End Sub

Hi Rory,

The purpose is the value range the entire workbook, so it kills all formulas, leaving only the values.There is some additional code that I pulled out to make my post simplier.

I basically wanted to use an array since I want to incorporate arrays in my code more.

Thanks.
 
Last edited:
Upvote 0
try something like this...

Code:
Sub rangevalue()
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        ws.Cells.Copy
        ws.Cells.PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
    Next ws
End Sub

Thanks. This is actucally what my code looks like now. I wanted to try and do this using an array instead. Not sure if this is possible.
 
Upvote 0
That's what the code I posted does. There's no point using an array here as you would need to loop to assign each sheet to the array, then loop again to manipulate each sheet.
 
Upvote 0
Ok fair enough. It definitely speeded up my code noticably compared to what I had prior to my array "experiment"
 
Upvote 0

Forum statistics

Threads
1,214,807
Messages
6,121,679
Members
449,047
Latest member
notmrdurden

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
Back
Top