+ Reply to Thread
Results 1 to 8 of 8

GoTo or Run Code???

Hybrid View

  1. #1
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    To call another macro from the 1st macro is easy

    Sub Macro1()
       Call Macro2
       if range("a1").value = 2 then
            call macro3
       end if
    End Sub
    Macro1 starts to run, calls macro2
    after MAcro2 runs macro1 continues running from the next command after it called macro2

    Note you do not need to use the word Call in front of the macro name
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  2. #2
    Registered User
    Join Date
    04-09-2008
    Posts
    5
    Thanks I see how that works but here is the code where I want to call the other Macro. I can call both my macros from another macro by themselves no problem. But for some reason it wont call the macro I have highlighted when I put the code in like this. Any thoughts???

    Sub Test()
    Dim requiredCells As Variant
    Dim prompts As Variant
    Dim i As Long
    
     requiredCells = Array("a3", "d3", "h3", "k3")
     prompts = Array("Date", "Account Manager", "Warehouse From", "Warehouse To")
    
     For i = LBound(requiredCells) To UBound(requiredCells)
         If Range(requiredCells(i)).Value = vbNullString Then
             Range(requiredCells(i)).Value = userInput(prompts(i))
             If Range(requiredCells(i)).Value = vbNullString Then Call Mail_workbook_Outlook_2
        End If
     Next i
    
    End Sub
    Function userInput(promptString As Variant) As String
    Do
        userInput = Application.InputBox("You must enter a " & promptString, Type:=2)
        If userInput = "False" Then userInput = vbNullString: Exit Function
    Loop Until userInput <> vbNullString
    End Function

  3. #3
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177
    My first guess would be a requirement is not being met? In this section of your code you use the same "If" statement twice.
     For i = LBound(requiredCells) To UBound(requiredCells)
         If Range(requiredCells(i)).Value = vbNullString Then
             Range(requiredCells(i)).Value = userInput(prompts(i))
             If Range(requiredCells(i)).Value = vbNullString Then Call Mail_workbook_Outlook_2
        End If
     Next i
    I would think that the string "If Range(requiredCells(i)).Value" is not evaluating to "vbNullString".

    A second look I see that you reset your "If" satement to "If Range(requiredCells(i)).Value = userInput(prompts(i))", then the second "If" statement would not evaluate to "vbNullString" and the call statement would be ignored.
    Last edited by Rick_Stanich; 04-23-2008 at 10:10 AM.
    Regards

    Rick
    Win10, Office 365

  4. #4
    Registered User
    Join Date
    04-09-2008
    Posts
    5
    I see what your saying this is how the code was originally written, I don't write very much VB code but have an understanding of how it works. It is set to exit the sub once it's finished checking all the cells for data. But what I need it to do is call the Email Macro so the excel file is then emailed out, that is where I am having the problem. I can't get it to go from this macro once its done with its routine to the email macro

    Thanks

    For i = LBound(requiredCells) To UBound(requiredCells)
         If Range(requiredCells(i)).Value = vbNullString Then
             Range(requiredCells(i)).Value = userInput(prompts(i))
             If Range(requiredCells(i)).Value = vbNullString Then Exit Sub: Rem Cancel pressed
         End If
     Next i
    
    End Sub

  5. #5
    Registered User
    Join Date
    04-09-2008
    Posts
    5
    Wooo hooo never mind my brain just exploded, I took out the second If statement and put in an else statement and NOW it works

    Thanks for everyones help on this, sometimes it helps to have a few extra eyes and brain cells

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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