Exit sub procedure

  • Thread starter Thread starter crapit
  • Start date Start date
C

crapit

How to make a sub routine exit totally when a it call another sub routine?

E.g if abc = 99 then filter sub routine will exit, but how do I make the
total() sub routine exit as well?

Sub Filter()
procedure...
if abc = 99
exit sub
end if
procedure...
procedure...
End Sub

Sub Total()
Filter()
Copy()
Page()
End Sub
 
You could make Filter a function that returns a Boolean ... True if
Filter completed False if it were prematurely terminated (exited).

Then in the Total() sub, it could be called something like

Function Filter() As Boolean
Filter = True ' initialise to the default
procedure...
if abc = 99
Filter = False
return Filter
exit sub
end if
procedure...
procedure...
return Filter
End Sub

Sub Total()
if Filter() then
Copy()
Page()
else
' do nothing else
end if
End Sub
 
Do i insert the following within the sub filter() ?
Function Filter() As Boolean
Filter = True ' initialise to the default
 
No. Replace
Sub Filter()
with
Function Filter() As Boolean

It should then say End Function instead of End Sub at its end. If not change
that too. A Function is just a Sub that returns a value. Or rather; a Sub is
just a Function that returns nothing ("Void" in other languages).

HTH. Best wishes Harald
 
Thank you.
Public lastrow As Long
How to set lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
available as public ?
 
Put
Public lastrow As Long
on top of a standard module, above the first Sub or Function. And nowhere
else, or you'll get a conflict.

HTH. Best wishes Harald
 
Then wat about
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row?

Wat's wrong with the following?
Sub add()
dim firstdate as range
If shtname = "small" Then
firstdate = "c1"
Else
firstdate = "b1"
End If
..Range(.Cells(1, 16), .Cells(last_row, 19)).copy _
Destination:=Worksheets(shtname).Range(firstdate)
end sub
 
"c1" is String, not Range.

Do you have Option Explicit on top of your modules ? You will get very
helpful error messages if you do.

Gotta run. Good luck.
Best wishes Harald
 
I removed the " " but it show variable not defined?
Must I insert
"lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row" at every
procedure?
 
crapit said:
I removed the " "

Why ? The datatype was wrong in your last posting. "c1" is not a range, but
c1 is not a range either. Until you decide wether you need a string variable
or a range variable I can't suggest how to do it.
but it show variable not defined?

Then maybe you havent declared lastrow as I told you to, or maybe you have a
typo. I'm working totally blindfolded here as I don't have any of your code
to look at and no idea of what it is supposed to do.
Must I insert
"lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row" at every
procedure?

No. That code finds and remembers the last row in active sheet's A column.
You should put it wherever you need to read what the last row is -once
initially to get a result different from row 0 (which doesn't exist), and
everytime after the last row changes; when a row is deleted, when something
new is inserted, ... and whenever "active sheet" changes from one to
another.

HTH. Best wishes Harald
 
Back
Top