PDA

View Full Version : Exit from main sub



Chandrasheka
10-29-2010, 02:11 AM
Hi,

I have main sub and I am checking conditions in another sub how to exit main sub. Please help me out on this.

Please find attached file.

Thanks

Chandra Shekar

Bob Phillips
10-29-2010, 02:56 AM
Use a function, along these lines


Sub First_Proc()

If fnTest Then

'do some stuff
End If
End Sub

Function fnTest()

'some code to do the test and return True if conditions met
End Function

slamet Harto
10-29-2010, 03:21 AM
Do you want to filter between date ? how about this

Sub Filterdate()
Dim Frmdt As Date, todt As Long
Frmdt = DateSerial(Day(Range("C3")), Month(Range("C3")), Year(Range("C3")))
todt = DateSerial(Month(Sheets(2).Range("C4")), Day(Sheets(2).Range("C4")), Year(Sheets(2).Range("C4")))
If Worksheets(2).AutoFilterMode = True Then
Worksheets(2).Range("A1").AutoFilter
End If
If ThisWorkbook.Worksheets(1).Cells(3, 3) <> "" Or ThisWorkbook.Worksheets(1).Cells(4, 3) <> "" Then
ThisWorkbook.Worksheets(2).Range("B1").AutoFilter field:=2, Criteria1:=">=" & todt
Else

Exit Sub

End If
End Sub

Chandrasheka
10-29-2010, 04:14 AM
Hi,

I need this task to be done without using Fuction.

Thanks

Bob Phillips
10-29-2010, 05:10 AM
Why?

Chandrasheka
10-29-2010, 05:14 AM
Sometime I want to return more than one value.

Thanks

Chandra Shekar

Bob Phillips
10-29-2010, 05:15 AM
Functions can return multiple values.

Chandrasheka
10-29-2010, 05:52 AM
Hi,

I am not aware of this. Please can you give a small example like this.

Thanks

Chandra Shekar

Bob Phillips
10-29-2010, 06:16 AM
Sub Caller()
Dim x, y

x = 17
y = 18
Call fnCalled(x, y)
MsgBox x & ", " & y
End Sub

Function fnCalled(ByRef arg1, ByRef arg2)

arg1 = 99
arg2 = 101
End Function

mikerickson
10-29-2010, 06:56 AM
Using End rather than Exit Sub will halt the entire process.
Sub Main()
' ...
Call otherRoutine()
' ...
End Sub

Sub otherRoutine()
' ...
If condition Then
End
End If
' ...
End Sub

Chandrasheka
10-29-2010, 07:07 AM
Thanks a lot for all.