Thanks Thanks:  0
Likes Likes:  0
Results 1 to 8 of 8

Thread: Conditional If Statement in Macro calling Sub Routine

  1. #1
    Board Regular
    Join Date
    Mar 2002
    Location
    North Wales Coast, UK
    Posts
    95
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hi again,
    Thank you for responses, I appreciate you taking the time to help.
    I notice from the examples posted the If, Then, Else Statement appears to draw a line under the Sub Routine and the conditions and in effect diverts to various other Sub Routines.
    I have code below a simple:

    If ActiveCell.Address = "$C$65536" Then Macro1

    Things are not working too well .. it appears that it performs Macro1 if the condition is True and then Returns to the original code below the If statement and executes that.
    I am wondering if I therefore have to do an If, Then, Else with a False condition returning to another Subroutine to finish the procedure?
    I am sorry if I am making 'heavy weather' of this but it the end of a long week!
    Regards
    S.

  2. #2
    Board Regular
    Join Date
    Mar 2002
    Location
    North Wales Coast, UK
    Posts
    95
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Evening all,

    I am currently trying to test to see if a Cell is, say C10 and if it is then 'branch' to another Subroutine. If the cell is not C10 I assume the If statement will be false and the 'original' macro will continue.

    Can anyone please give an example of an If statement. Many thanks in advance.
    S

  3. #3
    New Member
    Join Date
    Mar 2002
    Posts
    33
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    something like this
    if activecell=cells(10,3) then...
    might work, not sure...

  4. #4
    MrExcel MVP Jay Petrulis's Avatar
    Join Date
    Mar 2002
    Location
    Chicago, IL USA
    Posts
    2,040
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    On 2002-04-12 14:22, Sam40mUK wrote:
    Evening all,

    I am currently trying to test to see if a Cell is, say C10 and if it is then 'branch' to another Subroutine. If the cell is not C10 I assume the If statement will be false and the 'original' macro will continue.

    Can anyone please give an example of an If statement. Many thanks in advance.
    S
    Hi,

    Here is a simple example.
    Sub test()
    If ActiveCell.Address= "$C$10" Then
    test1
    Else
    test2
    End If
    End Sub

    Sub test1()
    MsgBox "TRUE"
    End Sub

    Sub test2()
    MsgBox "FALSE"
    End Sub

  5. #5
    Board Regular
    Join Date
    Mar 2002
    Location
    North Wales Coast, UK
    Posts
    95
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hi Jay,
    Once again thanx for your response to my posting, that was just what I was looking for a simple explanation.
    I am looking to complete the Subroutine and NOT return to the original macro. I have incorporated the If statement and it appears to return to the original macro, there again it could be me! As usual any advice would be appreciated.
    Cheers.
    S

  6. #6
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    On 2002-04-12 14:55, Sam40mUK wrote:
    Hi Jay,
    Once again thanx for your response to my posting, that was just what I was looking for a simple explanation.
    I am looking to complete the Subroutine and NOT return to the original macro. I have incorporated the If statement and it appears to return to the original macro, there again it could be me! As usual any advice would be appreciated.
    Cheers.
    S
    In the called macro just add End as your finishing statement

    eg.

    If vbYes = MsgBox("Select YES to Goto [Sheet1]" & vbCrLf _
    & "Select NO to Goto [Sheet2]", vbYesNo) Then
    Sheets("Sheet1").Select
    Else
    Sheets("Sheet2").Select
    End If
    End






    _________________
    Kind Regards,
    Ivan F Moala
    http://<font color="green"><a href="...r Construction

    [ This Message was edited by: Ivan F Moala on 2002-04-12 15:04 ]

  7. #7
    Board Regular
    Join Date
    Mar 2002
    Location
    North Wales Coast, UK
    Posts
    95
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hi All,
    Sorry to be a pain but anyone got any ideas?
    Cheers
    S

  8. #8
    MrExcel MVP Jay Petrulis's Avatar
    Join Date
    Mar 2002
    Location
    Chicago, IL USA
    Posts
    2,040
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hi,

    If ActiveCell.Address = "$C$65536" Then

    Macro1
    Exit Sub

    End if

    This will break out of the routine. It will run Macro1, then return to this calling sub and then exit it.

    HTH,
    Jay

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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