OnError Exit Sub Help ??

markh1182

New Member
Joined
Apr 25, 2006
Messages
48
Hi, I have the following code:
Code:
Sub SummarySheet2()

FinalRow = Sheets("Summary").Range("E50")
a = 2
x = Sheets.Count

For i = 1 To x

If Sheets("Detail " & i).Name = ("Detail " & i) Then

a = a + 1
    Sheets("Summary").Select
        Range("E" & a).Value = "Detail " & i & " Summary"
        Range("H" & a).Formula = "=sum('Detail " & i & "'!M:M)/2"
        Range("I" & a).Formula = "=sum('Detail " & i & "'!N:N)/2"
        Range("J" & a).Formula = "=sum('Detail " & i & "'!O:O)/2"

End If
Next i
Exit Sub

End Sub

If I leave the exit sub after next i, then this does what I want to start with but then fails. If I move the Exit Sub above End If then it does not error on me, but doesn't run properly. Any help would be much appreciated.
Thanks, Mark
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I don't know if this is what you mean:

Code:
Sub SummarySheet2() 

FinalRow = Sheets("Summary").Range("E50") 
a = 2 
x = Sheets.Count 

For i = 1 To x 

If Sheets("Detail " & i).Name = ("Detail " & i) Then 

a = a + 1 
    Sheets("Summary").Select 
        Range("E" & a).Value = "Detail " & i & " Summary" 
        Range("H" & a).Formula = "=sum('Detail " & i & "'!M:M)/2" 
        Range("I" & a).Formula = "=sum('Detail " & i & "'!N:N)/2" 
        Range("J" & a).Formula = "=sum('Detail " & i & "'!O:O)/2" 
Else
    Exit Sub
End If 

Next i 

End Sub
 
Upvote 0
still fails on the If statement. What it is I'm trying to do is sheet names are like Detail 1, Detail 2 etc. But sometime you could just have Detail 1 and sometimes it could be up Detail 5. I need something to capture all instances.
 
Upvote 0
Does this work for you?

Code:
Sub SummarySheet2()
Dim ws As Worksheet, sn As String
For Each ws In Worksheets
    If Left(ws.Name, 7) = "Detail " Then
        sn = ws.Name
        a = Val(Mid(sn, 8))
        With Sheets("Summary")
            .Range("E" & a) = "Detail " & a & " Summary"
            .Range("H" & a).Formula = "=sum('Detail " & a & "'!M:M)/2"
            .Range("I" & a).Formula = "=sum('Detail " & a & "'!N:N)/2"
            .Range("J" & a).Formula = "=sum('Detail " & a & "'!O:O)/2"
        End With
    End If
Next
End Sub
 
Upvote 0
Had to make one small amendment to your code Hotpepper but yep that seems to work.
Much Appreciated, Mark
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top