+ Reply to Thread
Results 1 to 8 of 8

Conditional macros

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-14-2020
    Location
    Pakistan
    MS-Off Ver
    15
    Posts
    123

    Conditional macros

    Hi,

    Is it possible for macro to run only if the other macro has been run fully ? I’ve multiple command button set in a worksheet, 1 to print AJ’s another one to clear all contents, is it possible for clear all contents macro should only work when print macro has been called fully. If print macro hasn’t been run fully then message box appears for the user to print documents first.

    Thank you in advance.

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,265

    Re: Conditional macros

    ??? e.g.:
    Option Explicit
    
    Private prmsn As Boolean
    
    Sub Macro1()
        prmsn = False
        
        On Error Resume Next
            'Some code
            
            'if error then an earlier exit from the procedure
            If Err.Number <> 0 Then Exit Sub
            
            'or other condition and an earlier exit from the procedure
            Dim co_pies As Integer
            
            'Some code
            
            If co_pies < 47 Then Exit Sub
            
            'Some code
        On Error GoTo 0
        
        'Some code
        
        prmsn = True
    End Sub
    
    Sub Macro2()
        If Not prmsn Then
            MsgBox "Macro1 has not been fully executed"
            Exit Sub
        End If
        
        'Some code
        
        prmsn = False
    End Sub

  3. #3
    Forum Contributor
    Join Date
    01-14-2020
    Location
    Pakistan
    MS-Off Ver
    15
    Posts
    123

    Re: Conditional macros

    Thank you - it’s not working for some reason
    Let me try and explain in detail

    Macro 1 is - print document
    Macro 2 is - clear document

    Both working fine - but I want to macro 2 to check if macro 1 has run fully before clearing the document. If macro 1 not run fully then stop the process and show message box.

    Thank you

  4. #4
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,265

    Re: Conditional macros

    Quote Originally Posted by samarbac View Post
    ... I want to macro 2 to check if macro 1 has run fully ...
    That's how it works.
    If the variable "prmsn" does not get the value "True" (prmsn = True) when executing "procedure_1", then "procedure_2" will not be executed.

    Quote Originally Posted by samarbac View Post
    it’s not working for some reason
    Give an example of your procedures.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Conditional macros

    Is Macro 2 always clear after Macro 1?

    If so
    Make Macro 2 as Private and run Macro 2 from Macro 1 after the print.

  6. #6
    Forum Contributor
    Join Date
    01-14-2020
    Location
    Pakistan
    MS-Off Ver
    15
    Posts
    123

    Re: Conditional macros

    Hi

    Thank you for the message- here is my 2 macros : I want 2nd macro to only works if 1st macro of PDF has fully run :

    Macro One :

    Sub CreatePDF_Click()
    
    pdfName = Sheets("Sheet1").Range("C1").Value
    ChDir "C:\User"
    fileSaveName = Application.GetSaveAsFilename(pdfName, _
    fileFilter:="PDF Files (*.pdf), *.pdf")
    If fileSaveName <> False Then
    Sheets(Array("Sheet1", "2", "3")).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fileSaveName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
    
    Sheets("Sheet1").Select
    Exit Sub
    End If
    End Sub

    2nd Macro


    Sub Newday()
    
    Sheets("Sheet1").Select
    With ActiveSheet
    .Unprotect
         With Range("A1:O40 ")
            .Locked = False
            .ClearContents
    End With
    .Protect
    End With
    Thank you

  7. #7
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,265

    Re: Conditional macros

    Try:
    Option Explicit
    
    Public prmsn As Boolean ' <= This declaration in the standard module
    
    Sub CreatePDF_Click()
        Dim pdfName As String, fileSaveName
        
        prmsn = False
        
        On Error GoTo the_end
        
        ChDir "C:\User"
        pdfName = Application.Trim(Sheets("Sheet1").Range("C1").Value)
        fileSaveName = Application.GetSaveAsFilename(pdfName, "PDF Files (*.pdf), *.pdf", , "Save as ...")
        If fileSaveName = False Then Exit Sub
        
        Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
        Sheets("Sheet1").ExportAsFixedFormat Type:=xlTypePDF, Filename:=fileSaveName, _
                                            Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                                            IgnorePrintAreas:=False, OpenAfterPublish:=True
        Sheets("Sheet1").Select
        
        If Dir(fileSaveName, vbNormal) = pdfName Then prmsn = True
        
    Exit Sub
    the_end:
    End Sub
    
    Sub Newday()
        If Not prmsn Then
            MsgBox "Procedure 'CreatePDF_Click' has not been fully executed"
            Exit Sub
        End If
        
        With Sheets("Sheet1")
            .Unprotect
            With .Range("A1:O40")
                .Locked = False
                .ClearContents
            End With
            .Protect
        End With
        
        prmsn = False
    End Sub
    Last edited by mjr veverka; 06-07-2020 at 11:06 AM.

  8. #8
    Forum Contributor
    Join Date
    01-14-2020
    Location
    Pakistan
    MS-Off Ver
    15
    Posts
    123

    Re: Conditional macros

    Hi

    thank you for the reply.

    Unfortunately, it's not doing anything - like when I try and save PDF - it brings up windows for location and when click save - it doesn't save. Also, when click on Newday macro it shows the message box of procedure not completed fully and then that's it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Conditional Formating and Macros
    By ashley72788 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-12-2016, 03:57 PM
  2. Conditional Macros
    By DragonRulerX in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-15-2015, 11:04 AM
  3. [SOLVED] Conditional formatting or macros or vba?
    By lcartwright in forum Excel General
    Replies: 2
    Last Post: 08-07-2014, 06:28 PM
  4. [SOLVED] Conditional formatting + macros
    By helpplease344 in forum Excel General
    Replies: 4
    Last Post: 05-16-2014, 03:42 PM
  5. [SOLVED] Macros and conditional formatting
    By skalus in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-25-2013, 08:38 AM
  6. [SOLVED] Help with Macros and/or Conditional Formatting.
    By Inez15 in forum Excel General
    Replies: 5
    Last Post: 11-08-2012, 01:11 PM
  7. Conditional Formatting And Macros
    By Dominic_Hodgson in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-04-2007, 05:33 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1