Category Fruits FruitsPrice Vegetables VegPrice Dairy DairyPrice
Fruits Apple $1.25 Tomato $0.99 Butter $3.45
FruitsPrice Grape $2.00 Potato $1.45 Cheese $4.75
Vegetables Pears $1.75 Onion $1.10 Milk $3.55
VegPrice Cucumber $0.75
Dairy
DairyPrice
I have the above table and 2 combo boxes; representing criteria 1) for "Category" (ROWS) and 2) for the "Items" under those categories (COLUMNS) and based on the 2 criteria in the ComboBoxes I would like to display the price in a TextBox using INDEX and MATCH if possible .
My vba code for the "textbox1" is:
Code
Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Set ws = Sheets("catitem")
ws.Select
MsgBox "The category in ComboBox1 is... " & Me.ComboBox1.Value & vbNewLine & _
"The item in ComboBox2 is... " & Me.ComboBox2.Value & vbNewLine & _
"The DATA RANGE is... " & Range("rng").Address & vbNewLine & _
"The HEADINGS range is... " & Range("items").Address
If ComboBox1 = "" Or ComboBox2 = "" Then
Exit Sub
End If
With Application.WorksheetFunction
TextBox1 = .Index(Range("rng"), _
.Match(Me.ComboBox1.Value, Range("items"), 0), _
.Match(Me.ComboBox2.Value, Range("rng"), 0)).Offset(, 1)
TextBox1.Value = Format(TextBox1, "$#,##0.00")
End With
End Sub
Display More
The above code obviously does not work
Can someone help me with the vba code. Thanks
The rest of my code is:
Code
Public ws As Worksheet, resp, cat, fruit, veg, bev, soap, title, listItem, catCol As String, nRow, iCnt, col As Integer, rng As Range, rate As Variant
Option Explicit
Private Sub ComboBox2_DropButtonClick()
With ComboBox2
.RowSource = Me.ComboBox1.Value
End With
Sheets("catitem").Select
With Application.WorksheetFunction
End With
End Sub
Private Sub ComboBox3_Change()
Set ws = Sheets("catitem")
ws.Select
With ComboBox3
Select Case ComboBox3
Case Is = Range("b1")
title = Range("b1").Value
ListBox1.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("fruits").RefersToRange)
Case Is = Range("c1")
title = Range("c1").Value
ListBox1.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("vegetables").RefersToRange)
Case Is = Range("d1")
title = Range("d1").Value
ListBox1.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("soaps").RefersToRange)
Case Is = Range("e1")
title = Range("e1").Value
ListBox1.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("beverages").RefersToRange)
Case Is = Range("f1")
title = Range("f1").Value
'MsgBox title
ListBox1.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("dairy").RefersToRange)
Case Is = Range("g1")
title = Range("g1").Value
ListBox1.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("pharmacy").RefersToRange)
Case Is = Range("h1")
title = Range("h1").Value
ListBox1.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("stationeries").RefersToRange)
End Select
End With
End Sub
Private Sub CommandButton1_Click()
Set ws = Sheets("catitem")
ws.Select
ListBox1.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("category").RefersToRange)
nRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
MsgBox "The NEXT EMPTY ROW is..." & nRow
Do
nRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
cat = Application.InputBox(prompt:="Enter a NEW CATEGORY...")
resp = MsgBox("Do you want to ADD this CATEGORY?", vbYesNo)
If resp = vbYes Then
Cells(nRow, 1) = cat
ListBox1.ListIndex = ListBox1.ListIndex
ListBox1.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("category").RefersToRange)
End If
resp = MsgBox("Do you want to CONTINUE...?", vbYesNo)
Loop While resp = vbYes
Columns.AutoFit
End Sub
Private Sub ComboBox1_Change()
On Error Resume Next
Set ws = Sheets("catitem")
ws.Select
Select Case ComboBox1
Case Is = Range("b1")
ComboBox2.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("fruits").RefersToRange)
Case Is = Range("c1")
ComboBox2.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("vegetables").RefersToRange)
Case Is = Range("d1")
ComboBox2.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("beverages").RefersToRange)
Case Is = Range("e1")
ComboBox2.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("soaps").RefersToRange)
Case Is = Range("f1")
ComboBox2.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("dairy").RefersToRange)
Case Is = Range("g1")
ComboBox2.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("pharmacy").RefersToRange)
Case Is = Range("h1")
ComboBox2.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("stationeries").RefersToRange)
End Select
ComboBox2.RowSource = Me.ComboBox1.Value
End Sub
Sub addItem()
Do
MsgBox "The column number is..." & col
nRow = Cells(Rows.Count, Val(col)).End(xlUp).Row + 1
MsgBox "The next empty ROW is..." & nRow
item = Application.InputBox(prompt:="Enter a new item...")
resp = MsgBox("Do you want to ADD this ITEM?", vbYesNo)
If resp = vbYes Then
'Do
Cells(nRow, col) = item
ListBox1.ListIndex = ListBox1.ListIndex
ListBox1.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names(title).RefersToRange)
End If
resp = MsgBox("Do you want to CONTINUE...?", vbYesNo)
Loop While resp = vbYes
Columns.AutoFit
End Sub
Private Sub CommandButton3_Click()
Unload Me
Range("b2").Select
End Sub
Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
With ListBox1
.RowSource = ComboBox3.Value
End With
End Sub
Display More