Input Box not stopping

hmltnangel

Active Member
Joined
Aug 25, 2010
Messages
290
Office Version
  1. 365
Platform
  1. Windows
Hi folks,

Hopefully a quick one :) I had a MsgBox pop up with the following code.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Range("D8") > Range("G8") Then
        MsgBox "Budget Exceeded by " & Abs(Range("D8") - Range("G8"))
    End If

End Sub

This worked fine.

I need to change it a little so that its an input box with a field for a user to complete. The data they complete will then populate a cell. SO I changed the code to

VBA Code:
rivate Sub Worksheet_Change(ByVal Target As Range)
    Dim myValue As Variant
    If Range("D8") > Range("G8") Then

        MsgBox "Budget Exceeded by " & Abs(Range("D8") - Range("G8"))
        myValue = InputBox("Explain why budget exceeded")
        Range("B10").Value = myValue
        
    End If

End Sub

WHich "works", but then doesnt stop after the user has input data.

Any suggestions for what im missing
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Maybe this ;
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo RecoverEvents
    Dim myValue As Variant
    If Range("D8") > Range("G8") Then
        MsgBox "Budget Exceeded by " & Abs(Range("D8") - Range("G8"))
        myValue = InputBox("Explain why budget exceeded")
        Application.EnableEvents = False
        Range("B10").Value = myValue
    End If
RecoverEvents:
    Application.EnableEvents = True
End Sub
 
Upvote 0
Solution
Quick change - is there a way to have the code run upon selection of the worksheet instead of when cell is changed?
 
Upvote 0
Use the "Worksheet_Activate" event instead, i.e.
VBA Code:
Private Sub Worksheet_Activate()

    Dim myValue As Variant
        
    If Range("D8") > Range("G8") Then
        MsgBox "Budget Exceeded by " & Abs(Range("D8") - Range("G8"))
        myValue = InputBox("Explain why budget exceeded")
        Application.EnableEvents = False
        Range("B10").Value = myValue
        Application.EnableEvents = True
    End If

End Sub
 
Upvote 0
Brilliant, the subtle change in that is where I was going wrong. I couldnt get the worksheet activate code to work. Now that comes up perfect.

Is there a way to format the returned value part below so that it is to two decimal places? (ie - £25.96 or similiar?)
VBA Code:
Abs(Range("D8") - Range("G8"))
 
Upvote 0
Brilliant, the subtle change in that is where I was going wrong. I couldnt get the worksheet activate code to work. Now that comes up perfect.

Is there a way to format the returned value part below so that it is to two decimal places? (ie - £25.96 or similiar?)
VBA Code:
Abs(Range("D8") - Range("G8"))
No need to use VBA for that part.
Just pre-format cell B10 to be your desired format, right on the worksheet (go to cell B10, right-click on it, go to Format Cells, go the Number tab and select/enter your desired cell format.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top