Complete noob question: How to copy and paste using VBA?

s_u_n

New Member
Joined
Apr 18, 2013
Messages
4
My first post here (I tried several examples, but nothing seems to work, so I registered here).
Here is the story:

I have a list of product names and prices in a sheet called MyList.

Now, I am building a calculator in another sheet (Calc) which needs to use the data from MyList
My Calc sheet has a dropdown list (I have used Data Validation)in cell B2.
I will use INDEX and MATCH or VLOOKUP or something else to get corresponding price displayed in C2.


Now the scenario is:
I expect the user to select any item from the drop down list (B2) and click on a command button "Add" which is located next to cell B2.
On click of the "Add" button, the product name and price should be copied from B2 and C2 to B5 and C5 respectively.
If the user presses the button with another product selected from the dropdown, the copy will still happen from B2 and C2, but paste has to go to the next blank row (which is B6 and C6). This process can continue for upto 250 rows (but in reality about 20 rows).


It is possible that after adding 5-10 items, a user may want to delete one or more items from the previous additions.
So he could simply go to a row, right click and delete any row manually. This should not break the functionality of the "Add" button in any way.

So, the problem is with the code for the "Add" button.



Private Sub CmdBut_Add_Click()

Dim NBR As Long 'Next Blank Row
With Sheets("Calc")

NBR = .Range("B" & .Rows.Count).End(xlUp).Row + 1
Worksheets("Calc").Range("B3").Value = .Range("B" & rw).Value
End With

End Sub

The line with NBR is probably correct if I use the Watch the value, but I have no clue how to copy and paste the values and repeat the process if required.
The line with Worksheets is incorrect in terms of syntax perhaps. Please ignore that one.

Thanks.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
please upload a sample file (with current data and desired result) on a sharing site, then paste the link here
 
Upvote 0
Code:
Sub a()
LR = Cells(17, "B").End(xlUp).Row + 1
Range("B" & LR) = Range("B2")
Range("C" & LR) = Range("C2")
End Sub
 
Last edited:
Upvote 0
Upvote 0
Code:
Private Sub BtAdd_Click()
LR = Cells(Rows.Count, "B").End(xlUp).Row
Set Rng = Range("B4:B" & LR).Find(What:="total", LookIn:=xlValues)
Rtotal = Rng.Row - 1
LR = Cells(Rtotal, "B").End(xlUp).Row + 1
Range("B" & LR) = Range("B2")
Range("C" & LR) = Range("C2")
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

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