Solved

Method range of object -Worksheet failed

Posted on 2016-07-30
5
74 Views
Last Modified: 2016-07-31
Hi

I have a macro that loops through each sheet in a workbook and copies certain ranges to a temporary sheet. As it loops through each worksheet, I would like it to store the name of the worksheet in cell AA1. I am using the sub below but keep getting the message "Method 'Range' of object '_worksheet' failed" at line 27. I'm not sure why, as all the other references to the worksheet variable 'projectsheet' seem to be working fine.

Dim nextrow As Integer
Dim i As Integer
Dim starttemp As Integer
Dim endrow As Integer

Sub resource1()
starttemp = 1
Dim projectsheet As Worksheet
Dim cel As Range
Dim temprows As Range
Dim startrow As Integer
Dim shtname As String
Dim rows As Integer
'Clear the contents of the temp sheet
Application.ScreenUpdating = False
For Each cel In Worksheets("Temp").Range("A1:T500")
cel.ClearContents
Next cel
Application.ScreenUpdating = True
Worksheets("Res Plan").Select
Range("A1").Select
'search each tab beyond tab 2
For Each projectsheet In Worksheets
If projectsheet.Index < 3 Then GoTo Next1
shtname = projectsheet.Name
MsgBox shtname
projectsheet.Range("aa:1").Value = shtname
'Find the Resource utilisation block on the projectsheet sheet
For Each cel In projectsheet.Range("A1:C200")
If cel.Value = "RESOURCE UTILISATION" Then GoTo Next2
Next cel
Next2:
startrow = cel.Row
endrow = startrow + 9
'Copy the range in blocks of 10
projectsheet.Range("B" & startrow & ":r" & endrow).Copy
Sheets("Temp").Range("A" & starttemp).PasteSpecial xlPasteValues
starttemp = starttemp + 9

Next1:
Next projectsheet
'clear formatting in temp sheet
For Each cel In Worksheets("Temp").Range("a1:r500")
cel.Interior.ColorIndex = xlNone
Next cel
'Remove empty rows
'For Each cel In Worksheets("Temp").Range("A1:a500")


'Set temprange = Worksheets("Temp").Range("a1:r40")
'rows = temprange.rows.Count
'For i = rows To 1 Step -1
'MsgBox i
'If WorksheetFunction.CountA(temprange.rows(i)) = 0 Then temprange.rows(i).Delete

'Next i

End Sub

Open in new window


Regards

Terry
0
Comment
Question by:Terrygordon
5 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 250 total points
ID: 41736004
Lose the : between aa and 1.
0
 
LVL 30

Assisted Solution

by:Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj) earned 125 total points
ID: 41736072
That line should be like this.....

projectsheet.Range("aa1").Value = shtname

Open in new window

0
 
LVL 18

Assisted Solution

by:Roy_Cox
Roy_Cox earned 125 total points
ID: 41736215
I've made a few suggestions that should speed up the code
Option Explicit


Dim nextrow As Long    ''/// best declare Rows as Long
Dim i As Integer
Dim starttemp As Integer
Dim endrow As Long    ''/// best declare Rows as Long

Sub resource1()
    starttemp = 1
    Dim projectsheet As Worksheet
    Dim cel As Range
    Dim temprows As Range
    Dim startrow As Long
    Dim shtname As String
    Dim rows As Long
    'Clear the contents of the temp sheet
    ''///not necessary
    'Application.ScreenUpdating = False
    Worksheets("Temp").Range("A1:T500").ClearContents
    ''/// no loop required
    'For Each cel In Worksheets("Temp").Range("A1:T500")
    'cel.ClearContents
    'Next cel
    'Application.ScreenUpdating = True

    ''/// no need to select
        'Worksheets("Res Plan").Select
        'Range("A1").Select
        
        'search each tab beyond tab 2
        For Each projectsheet In ThisWorkbook.Worksheets
            If projectsheet.Index < 3 Then GoTo Next1
            'MsgBox shtname
            projectsheet.Range("aa1").Value = shtname ''/// adds sheet name to AA1
            'Find the Resource utilisation block on the projectsheet sheet

            ''/// probably much faster to use .Find here
            For Each cel In projectsheet.Range("A1:C200")
                If cel.Value = "RESOURCE UTILISATION" Then GoTo Next2
            Next cel
Next2:
            startrow = cel.Row
            endrow = startrow + 9
            'Copy the range in blocks of 10
            projectsheet.Range("B" & startrow & ":r" & endrow).Copy
            Sheets("Temp").Range("A" & starttemp).PasteSpecial xlPasteValues
            starttemp = starttemp + 9

Next1:
        Next projectsheet
        'clear formatting in temp sheet
        'For Each cel In Worksheets("Temp").Range("a1:r500")
        'cel.Interior.ColorIndex = xlNone
        'Next cel
        Worksheets("Temp").Range("a1:r500").Interior.ColorIndex = xlNone
        'Remove empty rows
        'For Each cel In Worksheets("Temp").Range("A1:a500")


        'Set temprange = Worksheets("Temp").Range("a1:r40")
        'rows = temprange.rows.Count
        'For i = rows To 1 Step -1
        'MsgBox i
        'If WorksheetFunction.CountA(temprange.rows(i)) = 0 Then temprange.rows(i).Delete

        'Next i

    End Sub

Open in new window

0
 

Author Comment

by:Terrygordon
ID: 41736381
Thanks to all (and for the tips Roy). Can't believe I couldn't see something so obvious - I suppose that's why a fresh pair of eyes is so useful. :-)
0
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41736503
Pleased to help
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question