Inputbox on cancel delete initial action that prompted

Ace71425

Board Regular
Joined
Apr 20, 2015
Messages
130
See code below

Code:
Private Sub Worksheet_Change(ByVal TARGET As Range)
     
    Dim sReason As String
     
    If TARGET.Cells.Count > 1 Then Exit Sub
     
    Application.ScreenUpdating = False
     
    If Not Intersect(TARGET, Range("A1:A500")) Is Nothing Then
        If TARGET.Value = "MISC" Then
            sReason = Application.InputBox("Reason for MISC?", Type:=2)
            TARGET.Offset(0, 4) = sReason
        End If
    End If
     
    
    For I = 1 To 2000
        If Range("A" & I) = "MISC" And Range("E" & I) = "FALSE" Then
            Range("A" & I & ":E" & I).ClearContents
        End If
    Next I
     
    Application.ScreenUpdating = True
    
End Sub

I have this code that whenever a user picks MISC from a drop down validation in the A column it prompts them for a reason for choosing it which then pastes that reason to the subsequent E column. However when I click cancel it just puts FALSE in the E column and leaves the MISC. I tried using this...

Code:
For I = 1 To 2000
        If Range("A" & I) = "MISC" And Range("E" & I) = "FALSE" Then
            Range("A" & I & ":E" & I).ClearContents
        End If
    Next I

to delete the A column if they hit cancel but it isn't working. I dont want the user to be able to cancel out and leave the MISC box there. I want to basically force them to enter a reason. Thanks in advance!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Rich (BB code):
Private Sub Worksheet_Change(ByVal TARGET As Range)
     
    Dim sReason As String
     
    If TARGET.Cells.Count > 1 Then Exit Sub
     
    Application.ScreenUpdating = False
     
    If Not Intersect(Target, Range("A1:A500")) Is Nothing Then
        sReason = False
        If Target.Value = "MISC" Then
            While sReason = False
                sReason = Application.InputBox("Reason for MISC?", Type:=2)
            Wend
            Target.Offset(0, 4) = sReason
        End If
    End If
     
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
That's a step in the right direction. But I would like for them to be able to select cancel and have it delete the MISC in A if they picked it on error where that code just makes the cancel button unavailable. Also if you hit ok without any text it comes up with an error and allows them to leave the MISC without a reason which doesnt force them to do so.
 
Upvote 0
Code:
If Not Intersect(Target, Range("A1:A500")) Is Nothing Then
    sReason = ""
    If Target.Value = "MISC" Then
        While sReason = ""
            sReason = Application.InputBox("Reason for MISC?", Type:=2)
            If sReason = False Then
                Target.ClearContents
            Else
                Target.Offset(0, 4) = sReason
            End If
        Wend
    End If
End If
 
Upvote 0
Sorry to be a pain :( ...that code works when the cancel button is pushed but now it's not working when I enter text its coming up with an error and not pasting to E. also when I click ok with blank text it comes up with an error.
 
Upvote 0
Sorry to be a pain :( ...that code works when the cancel button is pushed but now it's not working when I enter text its coming up with an error and not pasting to E. also when I click ok with blank text it comes up with an error.

Do you mean the code is errorring out or it is showing #ERROR on the sheet?
If it is the code, can you please tell me the error message?
 
Upvote 0
The cancel button cancels out and deletes MISC from A perfectly but when I hit OK with or without any text in the input box it comes up with "run time error 13 type mismatch"
 
Upvote 0
The cancel button cancels out and deletes MISC from A perfectly but when I hit OK with or without any text in the input box it comes up with "run time error 13 type mismatch"

I believe that is a datatype mismatch... Are you declaring sReason somewhere in the code with a specific data type? if so, change the type to Variant. Also, does Column E use data validation?
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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