Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Method range of object -Worksheet failed

Posted on 2016-07-30
5
Medium Priority
?
131 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 35

Accepted Solution

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

Assisted Solution

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

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

Open in new window

0
 
LVL 22

Assisted Solution

by:Roy Cox
Roy Cox earned 500 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 22

Expert Comment

by:Roy Cox
ID: 41736503
Pleased to help
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
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…

876 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