ndendrinos
Well-known Member
- Joined
- Jan 17, 2003
- Messages
- 1,694
This code works well
In a row [A17:A35] I type a quantity sheet change event selects the next column to the right, a userform appears and I choose a “product”
.....A……B
1..10.…apple
2…5….pears
3…4….apple
because “apple” was chosen previously a msgbox(yes/no) appears saying that a same item was already chosen and offers to increase the previous quantity .
Here is the code:
The problems is that I don't know how to edit the code ever since I have added two new columns .
The setup of my sheet is now like this:
....A…B…C……B
1..10……..…apple
2…5…………..pears
3…4…………..apple
Columns B&D must not be cleared (they contain formulas that are needed.
Any solution is welcome especially if it is radically different than what's shown here.
Many thanks
In a row [A17:A35] I type a quantity sheet change event selects the next column to the right, a userform appears and I choose a “product”
.....A……B
1..10.…apple
2…5….pears
3…4….apple
because “apple” was chosen previously a msgbox(yes/no) appears saying that a same item was already chosen and offers to increase the previous quantity .
Here is the code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'this is to select adjacent cell after typing a quantity
If Not Intersect(Target, Range("A17:A35")) Is Nothing Then Target.Offset(0, 1).Select
'this in case user selects a duplicate item & gives the user the choice of adding same to the previous row or delete it
Dim rngFindRange As Range
Application.EnableEvents = False
If Intersect(Target, Range("A:B")) Is Nothing Or Me.UsedRange.Rows.Count = 1 Then GoTo TidyUp
If Target.Count > 1 Then
MsgBox "Please update only one cell at a time in columns A & B.", vbInformation
Application.Undo
GoTo TidyUp
End If
If Range("A" & Target.Row) <> "" And Range("B" & Target.Row) <> "" Then
Set rngFindRange = Range("B1:B" & Target.Row - 1).Find(What:=Range("B" & Target.Row), LookIn:=xlValues)
If Not rngFindRange Is Nothing Then
If MsgBox("Product exists on a previous row ... Add to it?", vbYesNo) = vbNo Then
Range("A" & Target.Row & ":E" & Target.Row).ClearContents
GoTo TidyUp
End If
rngFindRange.Offset(0, -1) = rngFindRange.Offset(0, -1) + Range("A" & Target.Row)
Range("A" & Target.Row & ":E" & Target.Row).ClearContents
End If
End If
Dim LastRow As Long
Range("A35").Select
LastRow = ActiveCell.End(xlUp).Offset(1, 0).Select
Range("A" & Target.Row & ":E" & Target.Row).ClearContents
Application.EnableEvents = True
TidyUp:
End Sub
The problems is that I don't know how to edit the code ever since I have added two new columns .
The setup of my sheet is now like this:
....A…B…C……B
1..10……..…apple
2…5…………..pears
3…4…………..apple
Columns B&D must not be cleared (they contain formulas that are needed.
Any solution is welcome especially if it is radically different than what's shown here.
Many thanks