ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   how to exit sub (https://www.excelbanter.com/excel-programming/347695-how-exit-sub.html)

xiang

how to exit sub
 

I got a macro something like
sub format()
application.run "Myworkbook.xls!import"
application.run "Myworkbook.xls!delrow"
application.run "Myworkbook.xls!delcol"
application.run "Myworkbook.xls!sort"
end sub

private sub import()
........
end sub
private sub delrow()
........
end sub
private sub delcol()
........
end sub
private sub sort()
........
end sub

the firest private sub is trying to open dialog box b
GetOpenFilename.
this macro runs good. My question is how can I exit the main sub when
the user click "cancel" button in openfile-dialog box rather than onl
exit the first private sub.

any help would be appreciate

--
xian
-----------------------------------------------------------------------
xiang's Profile: http://www.excelforum.com/member.php...fo&userid=2948
View this thread: http://www.excelforum.com/showthread.php?threadid=49207


Tom Ogilvy

how to exit sub
 
sub format()
Dim res as Variant
res = application.run( "Myworkbook.xls!import")
if vartype(res) = vbBoolean then exit sub
Workbooks.Open res
application.run "Myworkbook.xls!delrow"
application.run "Myworkbook.xls!delcol"
application.run "Myworkbook.xls!sort"
end sub

Function import() as Variant
Dim fName as Variant
fname = Application.GetOpenFileName()
Import = fName
end sub

sub delrow()
........
end sub
sub delcol()
........
end sub
sub sort()
........
end sub

If import, delrow, delcol and sort are in the same workbook as Format, then
you don't need to use appliction run, you can do

res = Import()
delrow
delcol
sort

I wouldn't use Format and Sort as procedure or function names as Excel/VBA
already have build in functions with these names.

--
regards,
Tom Ogilvy



"xiang" wrote in
message ...

I got a macro something like
sub format()
application.run "Myworkbook.xls!import"
application.run "Myworkbook.xls!delrow"
application.run "Myworkbook.xls!delcol"
application.run "Myworkbook.xls!sort"
end sub

private sub import()
.......
end sub
private sub delrow()
.......
end sub
private sub delcol()
.......
end sub
private sub sort()
.......
end sub

the firest private sub is trying to open dialog box by
GetOpenFilename.
this macro runs good. My question is how can I exit the main sub when
the user click "cancel" button in openfile-dialog box rather than only
exit the first private sub.

any help would be appreciated


--
xiang
------------------------------------------------------------------------
xiang's Profile:

http://www.excelforum.com/member.php...o&userid=29489
View this thread: http://www.excelforum.com/showthread...hreadid=492072




xiang[_2_]

how to exit sub
 

many thanks, Tom

but when I tried to run
res = application.run( "Myworkbook.xls!import")
res returns Nothing instead of True or False
I don't konw why. could you give any more hints?




Tom Ogilvy Wrote:
sub format()
Dim res as Variant
res = application.run( "Myworkbook.xls!import")
if vartype(res) = vbBoolean then exit sub
Workbooks.Open res
application.run "Myworkbook.xls!delrow"
application.run "Myworkbook.xls!delcol"
application.run "Myworkbook.xls!sort"
end sub

Function import() as Variant
Dim fName as Variant
fname = Application.GetOpenFileName()
Import = fName
end sub

sub delrow()
........
end sub
sub delcol()
........
end sub
sub sort()
........
end sub

If import, delrow, delcol and sort are in the same workbook as Format,
then
you don't need to use appliction run, you can do

res = Import()
delrow
delcol
sort

I wouldn't use Format and Sort as procedure or function names as
Excel/VBA
already have build in functions with these names.

--
regards,
Tom Ogilvy



"xiang" wrote in
message ...

I got a macro something like
sub format()
application.run "Myworkbook.xls!import"
application.run "Myworkbook.xls!delrow"
application.run "Myworkbook.xls!delcol"
application.run "Myworkbook.xls!sort"
end sub

private sub import()
.......
end sub
private sub delrow()
.......
end sub
private sub delcol()
.......
end sub
private sub sort()
.......
end sub

the firest private sub is trying to open dialog box by
GetOpenFilename.
this macro runs good. My question is how can I exit the main sub

when
the user click "cancel" button in openfile-dialog box rather than

only
exit the first private sub.

any help would be appreciated


--
xiang

------------------------------------------------------------------------
xiang's Profile:

http://www.excelforum.com/member.php...o&userid=29489
View this thread:

http://www.excelforum.com/showthread...hreadid=492072



--
xiang
------------------------------------------------------------------------
xiang's Profile: http://www.excelforum.com/member.php...o&userid=29489
View this thread: http://www.excelforum.com/showthread...hreadid=492072


xiang[_3_]

how to exit sub
 

thanks, Tom
your code works perfectly.
when import() is a function, res returns "false".
how about if the import() is a private sub, how could we let re
returns "false"?
res = application.run ( "Myworkbook.xls!import")
this seems it does not work, because res returns Nothing.

one more question, you said I can use the following codes if all sub i
same workbook,

res = Import()
delrow
delcol
sort

do you mean the following lines will work?

sub format()

res = Import()
if vartype(res) = vbBoolean then exit sub
Workbooks.Open res
delrow
delcol
sort

end sub


I will try.

appreciate your big help

--
xian
-----------------------------------------------------------------------
xiang's Profile: http://www.excelforum.com/member.php...fo&userid=2948
View this thread: http://www.excelforum.com/showthread.php?threadid=49207



All times are GMT +1. The time now is 01:19 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com