Solved

Method range of object -Worksheet failed

Posted on 2016-07-30
5
47 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 28

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 17

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 17

Expert Comment

by:Roy_Cox
ID: 41736503
Pleased to help
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now