+ Reply to Thread
Results 1 to 7 of 7

Insert values in cells (MsgBox)

Hybrid View

  1. #1
    Registered User
    Join Date
    05-01-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    85

    Insert values in cells (MsgBox)

    Hello

    I have a minor problem that I really hope one of you can help me with.

    Through a "problem solver process" in VBA I get some values that I want to insert in certain cells. The code is below (I am not giving the entire code as that seems to be unnecessary)

    ans = MsgBox("best solution found: RI = " & DBest & " given by i = " & iBest & " and j = " & jBest & vbNewLine _
    & vbNewLine & "Do you want to insert the found values in the sheet?", vbYesNo)
    
    If ans = vbNo Then
    Exit Sub
    
    ElseIf ans = vbYes Then
    Range("c2").Value = iBest
    Range("c3").Value = jBest
    End If
    
    Application.ScreenUpdating = True
    
    End Sub
    Problem is that nothing changes when I press yes in the messagebox? (even though the messagebox did say that optimal values were found)...

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Insert values in cells (MsgBox)

    Is this code specific to a particular sheet? Are you running it in a module or as a worksheet event?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    05-01-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    85

    Re: Insert values in cells (MsgBox)

    A module... And yes it is to a particular sheet. But basically I just get two values iBest and jBest that are shown in a messagebox - the code works that far. The problem occurs when I want the values to be shown in the cells C2 and C3 - the MsgBox does pose the question but nothing happens when I press yes :/

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Insert values in cells (MsgBox)

    You will need to declare the sheet names before the range. For e.g.
    Worksheets("Sheet1").Range("c2").Value = iBest

  5. #5
    Registered User
    Join Date
    09-17-2012
    Location
    Riyadh,KSA
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: Insert values in cells (MsgBox)

    hi lv27

    try this
    Sheets("Sheet1").Range("C2").Value = jBest
    Sheets("Sheet1").Range("C3").Value = jBest
    hope its help...^_^

  6. #6
    Forum Contributor
    Join Date
    09-05-2012
    Location
    It's more fun in the Philippines :D
    MS-Off Ver
    Excel 2007
    Posts
    209

    Re: Insert values in cells (MsgBox)

    Hi Lv27

    Please try this code below.

    If (MsgBox("best solution found: RI = " & DBest & " given by i = " & iBest & " and j = " & jBest & vbNewLine _
    & vbNewLine & "Do you want to insert the found values in the sheet?", vbYesNo)) = vbYes Then
        Range("c2").Value = iBest
        Range("c3").Value = jBest
    Else
        Exit Sub
    End If
    Hope it helps.

    Regards,
    thisisgerald
    Don't forget to mark your thread as [SOLVED].

  7. #7
    Registered User
    Join Date
    05-01-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    85

    Re: Insert values in cells (MsgBox)

    Thanks a lot for all the answers! I think the problem was that I had used Application.ScreenUpdating=False in the beginning of the code - and when I ran it with F8 I stopped before the "Applic....=True" part My bad, but thanks anyway!

+ 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