Solved

Method range of object -Worksheet failed

Posted on 2016-07-30
5
81 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 34

Accepted Solution

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

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 19

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 19

Expert Comment

by:Roy_Cox
ID: 41736503
Pleased to help
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel compare strings 6 55
NEED LOOK FOR NUM 23 29
vba code to create folders according to a hyperlink 14 41
Cell Manipulation 37 57
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

710 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