Avatar of Terrygordon
Terrygordon

asked on 

Method range of object -Worksheet failed

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
Microsoft Excel

Avatar of undefined
Last Comment
Roy Cox
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Terrygordon
Terrygordon

ASKER

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. :-)
Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

Pleased to help
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo