View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
Pls_Help_noob[_2_] Pls_Help_noob[_2_] is offline
external usenet poster
 
Posts: 5
Default Excel Formula to add stock

I wonder if you can help me again please. I got the macro working for one
pair of cells. However I want to apply the same model to a range of data. I
have tried the following:

Private Sub Worksheet_Change(ByVal Target As Range)
Set r1 = Range("E8:E78")
Set r2 = Range("F8:F78")
If Intersect(r1, Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
r2.Value = r2.Value + r1.Value
Application.EnableEvents = True
End Sub

After entering this macro i save the worksheet, reopen and try to enter the
data. When i enter the data i get a run-time error '13' and a message
asking me to debug. When i debug this row is highlighted "r2.Value =
r2.Value + r1.Value" I have opened the Help to try to solve this problem but
I can not resolve this issue.

Thanks for your time, I'm a complete Noob when it comes to this!

Regards Chris


Gary''s Student" wrote:

The following macro waits for changes in B7. After an entry is made, the
value will be added to the value currently in B2.

So enter 7 in B7 and B2 becomes 7.
Next enter 5 in B7 and B2 becomes 12.

Each time B7 is changed, B2 is updated:

Private Sub Worksheet_Change(ByVal Target As Range)
Set r1 = Range("B7")
Set r2 = Range("B2")
If Intersect(r1, Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
r2.Value = r2.Value + r1.Value
Application.EnableEvents = True
End Sub



Because it is worksheet code, it is very easy to install and automatic to use:

1. right-click the tab name near the bottom of the Excel window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you have any concerns, first try it on a trial worksheet.

If you save the workbook, the macro will be saved with it.


To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm





--
Gary''s Student - gsnu200758


"Pls_Help_noob" wrote:

Is it Possible to have have one target sell change one way only?

For example i want to use one cell only to add stock. What ever stock
arrives i just enter this number into the one cell and it changes. I have
the formula configured to change the stock held when stock in and stock out
change. But,


A B C D
1 PRODUCT NAME STOCK IN STOCK OUT STOCK HELD
2 Bananas 20 0 20
3 Apples 100 50 50
4 Pears
5
6 ADD STOCK ADD to STOCK IN
7 Bananas (enter number here)


I want to be able to enter the number of stock that i receive into one cell
so that all other cells change appropriately. For example if i get 20 more
bananas i want to enter 20 into cell "B7" so that this number will be added
to cell "B2". Thereafter if i change this number again when more stock comes
it adds again. Is this possible?