Run a macro by running a different macro

Joe9238

Board Regular
Joined
Jul 14, 2017
Messages
67
Hi, I have some code as seen below that I would like to be run part way through the code of another macro. How do you do this? Thanks in advance :)

Code:
Public Function CheckSheet( _ByVal SheetName As String _
   ) As Boolean
   
   For Each TargetSheet In wbTarget.Sheets
      If UCase(TargetSheet.Name) = UCase(Quote) Then
        doessheetexist = True
         GoTo linemarker1
         Exit For
      End If
   Next TargetSheet
   If doessheetexist = False Then GoTo linemarker2
End Function
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi,
to call a Function that returns a Boolean value from with your code you would do something like this

Code:
  If CheckSheet("Sheet1") Then
    
    'sheet exists do stuff
    
    Else
    
    MsgBox "Sheet Does Not Exist!"
    
    End If

Your CheckSheet function has one argument which requires the name of the worksheet to be passed as a string. If it exists, True is returned otherwise False.

HOWEVER - your function as written will not compile & even if it did, would only return a False result.

If you are just testing for a sheets existence within active workbook there are number of favoured ways amongst the forum of doing this - here is one I have added to your function.

Code:
Public Function CheckSheet(ByVal SheetName As String) As Boolean
    On Error Resume Next
    CheckSheet = CBool(Len(Worksheets(SheetName).Name) > 0)
    On Error GoTo 0
End Function

Dave
 
Last edited:
Upvote 0
Hi,
to call a Function that returns a Boolean value from with your code you would do something like this

Code:
  If CheckSheet("Sheet1") Then
    
    'sheet exists do stuff
    
    Else
    
    MsgBox "Sheet Does Not Exist!"
    
    End If

Your CheckSheet function has one argument which requires the name of the worksheet to be passed as a string. If it exists, True is returned otherwise False.

HOWEVER - your function as written will not compile & even if it did, would only return a False result.

If you are just testing for a sheets existence within active workbook there are number of favoured ways amongst the forum of doing this - here is one I have added to your function.

Code:
Public Function CheckSheet(ByVal SheetName As String) As Boolean
    On Error Resume Next
    CheckSheet = CBool(Len(Worksheets(SheetName).Name) > 0)
    On Error GoTo 0
End Function

Dave

The code works great. Thanks for the help!
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,695
Members
449,117
Latest member
Aaagu

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