To increase mouse clicked Cell value = previously mouse clicked cell value + 1

upan275

New Member
Joined
Dec 20, 2011
Messages
14
To ALL Macro-Men,:cool::cool:

Please allow myself to welcome in a New Regime of VBA!
Please help!!:rolleyes:

My ultimate Goal:eek:: To increase mouse clicked Cell value = previously mouseclicked cell value + 1 Range (A4:F57)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>

Initially Cellvalue will be first data-entered cell by user(i.e. “A4” need user input box for this) (Perhaps I cannot eliminate this step as user should enter first value, say A4= "5000")<o:p></o:p>

Previously clicked cell canbe any random cell in the same Range (“A4:F57") <o:p></o:p>

So far I have code:<o:p></o:p>

At present I am using DoubleClickEvent to copy “A4” value first & then increase value by right click event…(Not a real solution!)<o:p></o:p>

Any other Best Solution ???


Code:
[SIZE=3][FONT=Times New Roman]Private SubWorksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)<o:p></o:p>[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman] Target.Value = Range("A4").Value<o:p></o:p>[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman]If Target.Cells.Count >1 Then Exit Sub<o:p></o:p>[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman]If Not Intersect(Target,Range("myrange")) Is Nothing Then<o:p></o:p>[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman] Cancel = True <o:p></o:p>[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman]Target = Target + 1<o:p></o:p>[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman] End If<o:p></o:p>[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman]End Sub<o:p></o:p>[/FONT][/SIZE]
------------------------------------------------------------------------------------ 
[SIZE=3][FONT=Times New Roman]Private SubWorksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)<o:p></o:p>[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman]  If Target.Cells.Count > 1 Then Exit Sub<o:p></o:p>[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman]  If NotIntersect(Target, Range("myrange")) Is Nothing Then<o:p></o:p>[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman]  Cancel = True<o:p></o:p>[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman]  Target = Target + 1<o:p></o:p>[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman] End If   <o:p></o:p>[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman]End Sub<o:p></o:p>[/FONT][/SIZE]
[FONT=Times New Roman][SIZE=3][/CO[/SIZE][/FONT]DE]
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
try this:

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    On Error GoTo safeExit
    If Intersect(Target, Range("A4:F57")) Then
        If Not IsEmpty(Target) Then
            Application.EnableEvents = False
            Target.Value = Target.Value + 1
            Cancel = True
        End If
    End If
safeExit:
    Application.EnableEvents = True
End Sub
If the cell is empty, you'll be allowed to click into it as normal, so you can type in a number. If the value in the cell is non numeric, then nothing happens. I don't think you need to worry about the active range being > 1 cell as you don't seem to be able to double click multi cells, although I've not tested this exhaustively.
 
Last edited:
Upvote 0
Hi Weaver,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>

Thanks foryour Prompt Reply,
But it’snot what I am looking for…Your Code gives same result as my code inPrivateSubWorksheet_BeforeRightClick event<o:p></o:p>

Let me explainthe Problem again:<o:p></o:p>

I want toenter Ascending series number in Range (“A4: F57” = myrange ) ..Say start with3000, 3001….3002….3004…3006…. & so on. As I don’t want to type it all, Ienter ONLY first number of Series (Say A4=3000) & after this I use doubleclick event to enter Next number (in series) randomly in same range.<o:p></o:p>

Hence when Idouble-click any random cell in Range (A4:F57) after manually entering firstnumber (Say A4=3000), then VBA would remember Last clicked and/or Last userentered cell value & increase by +1and insert as Target value.<o:p></o:p>

For e.g.<o:p></o:p>

If A4 =3000 THEN Double clicked-randomly selected cell F50 results 3001<o:p></o:p>

If F50 =3001 THEN Double clicked-randomly selected cell C15 results 3002 <o:p></o:p>

If C4 = 3002THEN Double clicked-randomly selected cell B5 results 3003…Also if I doubleclick B5 TWICE it should results 3004..<o:p></o:p>
Now let’ssay I change “B5” Manually as 6004 Then next cell double clicking should Results6005..<o:p></o:p>

In short, currentlydouble clicked cell value = previously double-clicked cell value OR Previouslyuser entered cell value +1<o:p></o:p>

For this Ineed Excel to remember lastly selected cell value (which can be any cell in Range,Cell Locating with Offset doesn’t help in this case)<o:p></o:p>

BTW, I believethere must be some code in Excel to remember most recent 2-3 changes occurredin sheet. Otherwise Undo (Cntl+Z) can’t be possible.I tried toRecord Macro with Undo event but Macro doesn’t record undo code…<o:p></o:p>

This time I hope I explain it properly<o:p></o:p>

<o:p></o:p>
Well said:
“We can't solve problems by using the same kind of thinking we used when we createdthem.”-Albert Einstein<o:p></o:p>


<o:p><o:p></o:p></o:p>
 
Upvote 0
To be fair, that's hardly "Explaining the problem again", in fact I'd call it "explaining the problem"

If you want to 'remember' previous values, you can 'dim' variables outside your event handler. These variables will then maintain their values through different iterations of the handler. So if the handler changes such a value and is then called again, the variable will contain whatever you put in it last time. This is how you can 'remember' what was clicked, selected or updated from the previous activation of the handler.

HTH
 
Upvote 0
I don't think this is exactly what you're after, but hopefully it will demonstrate the principles.

Clear all the code out of your worksheet module and replace it with this:

Code:
Dim d As Double 'note this is outside of all the subroutines
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    On Error GoTo safeExit
    Debug.Print d
    If Not Intersect(Target, Range("A4:F57")) Is Nothing Then
        Target.Value = d
        Cancel = True
    End If
safeExit:
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not IsEmpty(Target) Then
        d = Target.Value + 1
        Debug.Print Target.Address, d
    End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not IsEmpty(Target) Then
        d = Target.Value + 1
    End If
End Sub
Now, if you click or select a cell with a number in it, that number is recorded in d, which doesn't lose its value once the subroutine ends. When you click on an empty cell the value of the cell, plus 1 should appear in the empty cell. If you select an empty cell and type a number in it, that value gets recorded and updated also.

Note, it could probably do with a bit more error trapping.
 
Upvote 0
BRILLAINTLY WEAVED CODE WEAVER!!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>

When first time I select more than one cell (just to delete &re-enter), I got error “13” , was easy fixed with: On Error GoTosafeExit<o:p></o:p>

Also added Rightclick event to reduce number.<o:p></o:p>
Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
 If Not Intersect(Target, Range("A4:F57")) Is Nothing Then
        Target.Value = Target.Value - 1
        Cancel = True
End Sub
<o:p></o:p>
Thanks a bunch!!
:):):)

<o:p></o:p>

<o:p></o:p>
 
Upvote 0
No problem, glad for the feedback, and that you were able to modify & adapt it for your needs.

Happy Christmas
 
Upvote 0
PS you probably alrready figured this out, but you can take out the 'debug.print' statements - these were just for my benefit while I was coding.
 
Upvote 0

Forum statistics

Threads
1,220,933
Messages
6,156,937
Members
451,386
Latest member
leolagoon94

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