I have modified your code as follows to suite my need
Code: Select all
Private Sub Worksheet_Change(ByVal Target As Range)
Dim oCell As Range
Dim lngTargetRow As Long
Dim lngSourceRow As Long
Const lngMinRow = 4 ' First data row in Price Range sheet.
Dim lngMaxRow As Long
Dim strFruit As String
Dim wshSource As Worksheet
If Not Intersect(Target, Range("B12,B13")) Is Nothing Then ' You can Change the range here
Application.EnableEvents = False
' Clear G18:G1000
Range("G18:G1000").ClearContents
If Range("B12") = "" Or Range("B13") = "" Then
' Can'lngTargetRow determine the price range - do nothing
Else
Set wshSource = Worksheets("Price Range")
lngMaxRow = wshSource.Range("A" & wshSource.Rows.Count).End(xlUp).Row
For lngTargetRow = 18 To 20
strFruit = Range("A" & lngTargetRow)
For lngSourceRow = lngMinRow To lngMaxRow
If wshSource.Range("A" & lngSourceRow) = strFruit And _
wshSource.Range("C" & lngSourceRow) <= Range("B12") And _
wshSource.Range("D" & lngSourceRow) >= Range("B12") Then
Select Case Range("B13")
Case "Male"
Range("G" & lngTargetRow) = wshSource.Range("E" & lngSourceRow)
Case "Female"
Range("G" & lngTargetRow) = wshSource.Range("H" & lngSourceRow)
End Select
Exit For
End If
Next lngSourceRow
Next lngTargetRow
End If
Application.EnableEvents = True
End If
End Sub
The following code copies the product name from the sheet name mentioned in the code to the Receipt sheet.
How could I combine the following code to the above code so that when the product name is written in column A the appropriate price range for the product gets copied from the above code when the "sex" and "age" is changed accordingly.
Code: Select all
Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "H9" '<== change to suit
Dim LastRow As Long
Dim nextRow As Long
Dim Category As String
Dim i As Long
On Error GoTo ws_exit
Application.EnableEvents = False
If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
Me.Range("A15").Resize(1000, 7).ClearContents
nextRow = 14
With Worksheets("OrderData")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 5 To LastRow
If .Cells(i, "I").Value2 = Target.Value Then
nextRow = nextRow + 1
If .Cells(i, "D").Value2 <> Category Then
nextRow = nextRow + 1
Category = .Cells(i, "D").Value2
Me.Cells(nextRow, "D").Value2 = Category
Me.Cells(nextRow, "D").Font.Bold = True
nextRow = nextRow + 1
Me.Cells(nextRow, "A").Value2 = "Product Name"
Me.Cells(nextRow, "A").Font.Bold = True
Me.Cells(nextRow, "E").Value2 = "Sale"
Me.Cells(nextRow, "E").Font.Bold = True
Me.Cells(nextRow, "F").Value2 = "Units"
Me.Cells(nextRow, "F").Font.Bold = True
Me.Cells(nextRow, "G").Value2 = "Price Range"
Me.Cells(nextRow, "G").Font.Bold = True
nextRow = nextRow + 1
End If
Me.Cells(nextRow, "A").Value2 = .Cells(i, "E").Value2
Me.Cells(nextRow, "A").Font.Bold = False
Me.Cells(nextRow, "F").Formula = "=VLOOKUP(A" & nextRow & _
",PriceRange!$A$3:$D$99,2 ,FALSE)"
Me.Cells(nextRow, "G").Font.Bold = False
End If
Next i
End With
End If
Dim m As Long
With Worksheets("Reciept")
m = .Cells(.Rows.Count, 1).End(xlUp).Row
If Not .Cells(m, 4) = "Thank You" Then
.Cells(m + 1, 4) = "Thank You"
End If
End With
ws_exit:
Application.EnableEvents = True
End Sub
Any help would be kindly appriciated.