Double click one cell to add data to next empty row in different worksheet

Rextus

New Member
Joined
Jun 23, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I'm trying to make sort of a shopping cart. The goods are on a sheet called List and start from row 2 to row 594. There is a cell besides each item (range "E2:E594"). When I double click the cell, it transfers the data in the cells to the left to another worksheet called Cart and displays a message. The code below is what I cobbled together.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("E2:E594")) Is Nothing Then Exit Sub
Sheets("Cart").Range("D34") = Cells(Target.Row, 5)
Sheets("Cart").Range("C34") = Cells(Target.Row, 4)
If Target.Locked Then
Range("A1").Activate
MsgBox "Item added to Cart successfully", vbInformation, "Cart updated"
End If
End Sub

This code works but only for the first row of the Cart worksheet (row 34). If I then go back to the List worksheet and "add" another item by clicking another row, that new selection overrides the information in row 34 of the Cart worksheet. I'd like to have the info from the second item inserted to the 35th row in cells C35 and D35. Can anyone help?

TIA
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Please tell me in specifics what your ultimate Goal is:
I know you want to double click on a cell and or cells to perform a task
Do not say read my code.
I see the code you have here, but it is hard to see the ultimate Goal
And what does this mean:
If Target.Locked Then

So, do you have a row full of shopping items you want copied to another sheet?
So in Range E2 you have "Bread" and in Range F2 you have "Soup"
And you want this data copied to a sheet named "Cart"
 
Upvote 0
Please tell me in specifics what your ultimate Goal is:
I know you want to double click on a cell and or cells to perform a task
Do not say read my code.
I see the code you have here, but it is hard to see the ultimate Goal
And what does this mean:
If Target.Locked Then

So, do you have a row full of shopping items you want copied to another sheet?
So in Range E2 you have "Bread" and in Range F2 you have "Soup"
And you want this data copied to a sheet named "Cart"
>And what does this mean:
>If Target.Locked Then

The cells I want to double click will be locked. This is to prevent accidentally editing them after the double click, and coincidentally, I can also use Target.Locked to check if the cell is locked, so I don't get random stuff copied into the Cart worksheet whenever I want to edit the product info.


Here's an example of what the List worksheet contains:

CategoryCan spoilBrandProductDouble click to add to cart
GroceriesYesBreadyBreadAdd Bready Bread to cart
FluidsNoSoupySoupAdd Soupy Soup to cart

And yes, if I double click "Add Bready Bread to cart", then double click "Add Soupy Soup to cart", I want to have the two items added sequentially into the Cart worksheet. I only need Brand and Product in the cart, not the data in the entire row.

Let me know if you need more info.

Thanks!
 
Upvote 0
Try this:
Now this script does not lock or unlock
Add that to the script if you want

This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Modified  6/23/2022  7:09:54 PM  EDT
If Target.Column = 5 Then
Cancel = True
Application.ScreenUpdating = False
Dim r As Long
Dim Lastrow As Long
Lastrow = Sheets("Cart").Cells(Rows.Count, "A").End(xlUp).Row + 1
r = Target.Row
Cells(r, 3).Resize(, 2).Copy Sheets("Cart").Cells(Lastrow, 1)
Application.ScreenUpdating = True
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
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