|
-
Jul 5th, 2012, 06:18 AM
#1
Thread Starter
Lively Member
[RESOLVED] set value of cell to 0 based on value of another cell in a TABLE range
Hi
I have a TABLE range which has 3 columns. Column 1 is called 'Product', Column 2 is called 'Price' and column 3 is called 'Discount'.
I only want a value in one of these cells for each product so basically a user cannot have both a price and discount.
I need some code that will set the discount value to 0 on the same row if the user has entered a price value. Same if the user has entered a value in the discount column - I want the price on the same row to be set to 0.
I am thinking of using the Worksheet_Change event for this so that the update happens immediately.
Thanks for any suggestions on how to best go about this.
Last edited by scoobster; May 10th, 2013 at 03:22 AM.
Reason: resolved
-
Jul 5th, 2012, 06:31 AM
#2
Re: set value of cell to 0 based on value of another cell in a TABLE range
using the Worksheet_Change event
try like
vb Code:
if target.column= 2 and not target = 0 then target.offset(,1) = 0 elseif target.column = 3 and not target = 0 then target.offset(, -1) = 0 end if
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 5th, 2012, 07:59 AM
#3
Thread Starter
Lively Member
Re: set value of cell to 0 based on value of another cell in a TABLE range
Excellent code. Worked first time and does exactly what I was looking for.
Thanks a lot.
-
Jul 10th, 2012, 09:58 AM
#4
Thread Starter
Lively Member
Re: set value of cell to 0 based on value of another cell in a TABLE range
Sorry to come back on this but I have a small issue with the slighly modified code.
I have validation setup on cells in column 6 such that I can only enter a number between 0 and 100. When I enter, say, 100.5 then I get a validation failed message but if I accidently press ESC key then I get error "1004 Application Defined or object defined" on the line;
Code:
Target.Offset(, 1) = 0
Full code is below;
Code:
Private Sub Worksheet_Change(ByVal Target As range)
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub 'if multiple cells are selected then exit to avoid an error
If Target.Column = 6 And Not Target = 0 Then 'if discount column has been updated and is not 0 then set price cell to 0
Target.Offset(, 1) = 0
Else
If Target.Column = 7 And Not Target = 0 Then 'if price column has been updated and is not 0 then set discount cell to 0
Target.Offset(, -1) = 0
End If
End If
End Sub
Any idea how to trap / stop this error?
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|