Hello everybody,
I am currently managing a stock (inventory management) portfolio in Excel. In this, I have my current inventory quantity in column I and two columns in column J & K which I can use to subtract & add amounts from my quantity, so:
Column I = Current quantity
Column J = Enter any number and this will be subtracted from the quantity in column I, after which the number will disappear from column J, thus leaving new room for a new number to be entered in the future.
Column K = Enter any number and this will be added from the quantity in column I, after which the number will disappear from column J, thus leaving new room for a new number to be entered in the future.
My current VBA code looks like this:
For good measurement, I have it copy pasted here too:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, c As Range
Set r = Intersect(Target, Columns("J:K"))
If Not r Is Nothing Then
Application.EnableEvents = False
For Each c In r
With Cells(c.Row, "I")
If IsNumeric(.Value) Then
.Value = .Value + IIf(c.Column = 10, -c.Value, c.Value)
Cells(c.Row, "L").Value = Now
Cells(c.Row, "L").NumberFormat = "dd-mm-yyyy, hh:mm:ss"
c.ClearContents
End If
End With
Next c
Application.EnableEvents = True
End If
End Sub
I was given this code to me by another member on this forum some time ago and it works wonders. However, I have trouble understanding some aspects of it. Specifically, in the 8th line, I don't understand what the number "10" means in this sentence:
.Value = .Value + IIf(c.Column = 10, -c.Value, c.Value)
Can anybody please help me understand this? I want to understand in case I want to move my formula from the current columns to future ones somewhere else in the sheet.
Thank you so much everybody!
PS. If anybody can explain to me what application.enableevents = true or = false means, that would also be greatly appreciated as well as the "Dim r as range, c as range" parts.
Best regards,
David
I am currently managing a stock (inventory management) portfolio in Excel. In this, I have my current inventory quantity in column I and two columns in column J & K which I can use to subtract & add amounts from my quantity, so:
Column I = Current quantity
Column J = Enter any number and this will be subtracted from the quantity in column I, after which the number will disappear from column J, thus leaving new room for a new number to be entered in the future.
Column K = Enter any number and this will be added from the quantity in column I, after which the number will disappear from column J, thus leaving new room for a new number to be entered in the future.
My current VBA code looks like this:
For good measurement, I have it copy pasted here too:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, c As Range
Set r = Intersect(Target, Columns("J:K"))
If Not r Is Nothing Then
Application.EnableEvents = False
For Each c In r
With Cells(c.Row, "I")
If IsNumeric(.Value) Then
.Value = .Value + IIf(c.Column = 10, -c.Value, c.Value)
Cells(c.Row, "L").Value = Now
Cells(c.Row, "L").NumberFormat = "dd-mm-yyyy, hh:mm:ss"
c.ClearContents
End If
End With
Next c
Application.EnableEvents = True
End If
End Sub
I was given this code to me by another member on this forum some time ago and it works wonders. However, I have trouble understanding some aspects of it. Specifically, in the 8th line, I don't understand what the number "10" means in this sentence:
.Value = .Value + IIf(c.Column = 10, -c.Value, c.Value)
Can anybody please help me understand this? I want to understand in case I want to move my formula from the current columns to future ones somewhere else in the sheet.
Thank you so much everybody!
PS. If anybody can explain to me what application.enableevents = true or = false means, that would also be greatly appreciated as well as the "Dim r as range, c as range" parts.
Best regards,
David