Solved

Method range of object -Worksheet failed

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

948 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

20 Experts available now in Live!

Get 1:1 Help Now