Consulting

Results 1 to 6 of 6

Thread: Solved: Message box code

  1. #1
    VBAX Contributor
    Joined
    Dec 2006
    Posts
    193
    Location

    Solved: Message box code

    The code below shows a message box if a criteria is not met ( In this case cell V5 being blank). It allows the user the option to continue or not . I need help with further code so that if cell V5 is not blank the macro will run (ie Copy and paste a selection of data) without showing any message box.

    Thanks for any help you can offer
    [VBA]
    Sub Button4_Click()
    '
    ' Button4_Click Macro
    If IsEmpty(Range("v5").Value) Then

    Dim strAnswer As VbMsgBoxResult
    strAnswer = MsgBox("WARNING NO CLOSING STOCK HAS BEEN ENTERED ,IF YOU CONTINUE THERE WILL BE NO OPENING FOR NEW REPORT ?", vbQuestion + vbYesNo, "Decision time!")
    If strAnswer = vbYes Then



    '
    Range("V5:V240").Copy Range("C5")

    Range("D5:Q240,V5:W240").Select
    Range("V5").Activate ' Not sure why this is here
    Application.CutCopyMode = False
    Selection.ClearContents
    Selection.ClearComments
    End If
    End If
    End Sub
    [/VBA]

  2. #2
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Hi,

    Try this:

    [VBA]
    Sub Button4_Click()
    '
    ' Button4_Click Macro
    If IsEmpty(Range("v5").Value) Then

    MsgBox("WARNING NO CLOSING STOCK HAS BEEN ENTERED ,IF YOU CONTINUE THERE WILL BE NO OPENING FOR NEW REPORT ?", vbQuestion + vbYesNo, "Decision time!")
    Else
    Range("V5:V240").Copy Range("C5")
    Range("D5:Q240,V5:W240").Select
    Range("V5").Activate ' Not sure why this is here
    Application.CutCopyMode = False
    Selection.ClearContents
    Selection.ClearComments
    EndIf

    End Sub
    [/VBA]

  3. #3
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Reverse the logic.
    [VBA]Sub testvalue()
    If Not IsEmpty(Range("V5").Value) Then
    Range("V5:V240").Copy Range("C5")
    Else
    MsgBox ("Nothing to copy")
    End If
    End Sub[/VBA]
    Charlize

  4. #4
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    Nevermind, I misunderstood what you were looking for.
    The most difficult errors to resolve are the one's you know you didn't make.


  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub Button4_Click()
    '
    ' Button4_Click Macro
    If IsEmpty(Range("V5").Value) Then
    Dim strAnswer As VbMsgBoxResult
    strAnswer = MsgBox("WARNING NO CLOSING STOCK HAS BEEN ENTERED, " & _
    "IF YOU CONTINUE THERE WILL BE NO OPENING FOR NEW REPORT ?", _
    vbQuestion + vbYesNo, "Decision time!")
    If strAnswer = vbYes Then DoCopy
    Else
    DoCopy
    End If
    End Sub

    Sub DoCopy()
    Range("V5:V240").Copy Range("C5")
    With Range("D5:Q240,V5:W240")
    .ClearContents
    .ClearComments
    End With
    Application.CutCopyMode = False
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Contributor
    Joined
    Dec 2006
    Posts
    193
    Location
    Thank you all for your help you guys are great ,mdmackillop your code is perfect

Posting Permissions

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