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

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

fabriciofcmiranda

Board Regular
Joined
Jun 27, 2006
Messages
62
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
 

markh1182

New Member
Joined
Apr 25, 2006
Messages
48
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.
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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
 

markh1182

New Member
Joined
Apr 25, 2006
Messages
48
Had to make one small amendment to your code Hotpepper but yep that seems to work.
Much Appreciated, Mark
 

Forum statistics

Threads
1,140,940
Messages
5,703,285
Members
421,289
Latest member
fbohlandt

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
Top