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

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 "Goods" 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" on row 34 and displays a message.

I have a partially working script but only for the first row of the Cart worksheet (row 34). See the code below if it can be of any help.

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
 'Check if double clicked cell is within column E of Goods spreadsheet
    If Intersect(Target, Range("E2:E594")) Is Nothing Then Exit Sub
'Copy data from Goods to Cart
    Sheets("Cart").Range("D34") = Cells(Target.Row, 5) 
    Sheets("Cart").Range("C34") = Cells(Target.Row, 4) 
    If Target.Locked Then 'Only display message is cells in column E of Goods spreadsheet is pressed
        Range("A1").Activate
        MsgBox "Item code pasted to Spare Parts worksheet successfully", vbInformation, "Paste success"
    End If
End Sub

The problem is that 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.

Example:

"Goods" spreadsheet
ABCDE (these cells are locked to prevent modification)
1CategoryBrandProductCan spoilDouble click to add item description to cart
2GroceriesBreadyBreadYes=CONCAT(B2," ",C2)
3FluidsSoupySoupNoSoupy Soup
4PPEGloveyGloveNoGlovey Glove

"Cart" spreadsheet
ABCD
33Item numberIn stockCan spoilItem Description
34Item 1
35Item 2
36Item 3

When I double click on cell E3 in the "Goods" spreadsheet, I want to have the following in "Cart"

ABCD
33Item numberIn stockCan spoilItem Description
34Item 1NoSoupy Soup
35Item 2
36Item 3

If I go back and double click on cell E4 in the "Goods" spreadsheet, I want to have the following in "Cart"

ABCD
33Item numberIn stockCan spoilItem Description
34Item 1NoSoupy Soup
35Item 2NoGlovey Glove
36Item 3

Using the script I have currently (shown above), row 34 will get overwritten. I'd like to have the script write to row 35 instead. Please let me know if you need more explanations.

TIA.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Does this do what you want?
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    Dim nr As Long

'Check if double clicked cell is within column E of Goods spreadsheet
    If Intersect(Target, Range("E2:E594")) Is Nothing Then Exit Sub

'Find next available row on "Cart" sheet
    nr = Sheets("Cart").Cells(Rows.Count, "C").End(xlUp).Row + 1
    If nr < 34 Then nr = 34
    
'Copy data from Goods to Cart
    Sheets("Cart").Cells(nr, "D") = Cells(Target.Row, 5)
    Sheets("Cart").Cells(nr, "C") = Cells(Target.Row, 4)
    If Target.Locked Then 'Only display message is cells in column E of Goods spreadsheet is pressed
        Range("A1").Activate
        MsgBox "Item code pasted to Spare Parts worksheet successfully", vbInformation, "Paste success"
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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