Help with vba code

kalumbair

New Member
Joined
Aug 2, 2018
Messages
35
Hello All,
I was wondering if anyone could help me with this. I've been trying to figure it out for days now, but just can't. below I have a code which allows me to change the value in a cell with a double click. I can do this in any cell in the worksheet. the problem is, I only want to be able to do this in a specific column and no where else in the worksheet. Any assistance is greatly appreciated.



Best Regards,
Ox


Private Sub Worksheet_BeforeDoubleClick(<wbr style="display: inline-block;">ByVal Target As Range, Cancel As Boolean)
ActiveCell.Select
myVar = InputBox("ENTER THE QUANTITY IN THE SPACE PROVIDED (Include a (-) Sign if the QTY is out Going Stock)", "Enter Quantity")
Dim answer As VbMsgBoxResult
answer = MsgBox("Is the Quantity Entered Correct?", vbYesNo, "Enter QTY")
If answer = vbYes Then
ActiveCell.Value = myVar + ActiveCell.Value
End If
If answer = vbNo Then
myVar = InputBox("Enter the Correct Quantity", "Buy or Sell")
ActiveCell.Value = myVar + ActiveCell.Value
End If

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

ActiveCell.Select


[COLOR=#ff0000]If ActiveCell.Column = 4 Then[/COLOR]


    myVar = InputBox("ENTER THE QUANTITY IN THE SPACE PROVIDED (Include a (-) Sign if the QTY is out Going Stock)", "Enter Quantity")
    
    Dim answer As VbMsgBoxResult
    
    answer = MsgBox("Is the Quantity Entered Correct?", vbYesNo, "Enter QTY")
    
    If answer = vbYes Then
        ActiveCell.Value = myVar + ActiveCell.Value
    End If
    
    If answer = vbNo Then
        myVar = InputBox("Enter the Correct Quantity", "Buy or Sell")
    ActiveCell.Value = myVar + ActiveCell.Value
    End If
[COLOR=#ff0000]End If[/COLOR]


End Sub

This should suffice. You didn't mention what column you want to fire the macro, so I picked D, but if you need other just change column number to other in this part:

If ActiveCell.Column = 4 Then


 
Upvote 0
Another option
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   Dim MyVar As Variant
   If Target.Column <> [COLOR=#ff0000]2[/COLOR] Then Exit Sub
   Cancel = True
   MyVar = InputBox("ENTER THE QUANTITY IN THE SPACE PROVIDED (Include a (-) Sign if the QTY is out Going Stock)", "Enter Quantity")
   If MsgBox("Is the Quantity " & MyVar & " Entered Correct?", vbYesNo, "Enter QTY") = vbYes Then
      Target.Value = MyVar + Target.Value
   Else
      MyVar = InputBox("Enter the Correct Quantity", "Buy or Sell")
      Target.Value = MyVar + Target.Value
   End If
End Sub
I've used column B, change the value in re to suit
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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