combining Sumif with Vlookup on Excel VBA

jeck32

New Member
Joined
Sep 23, 2022
Messages
9
Office Version
  1. 2016
Hi Gurus,

I would need your help on building a code that combines SUMIF and Vlookup on Excel VBA.

I have a Table named StoreRecords, with one column for Store Locations and another column for Daily Sales.

Now, what I'm trying to accomplish is to create a userform with combo box that will return unique store names and one textbox or label text that will display its total sales for the month

ex.
StoreRecords
Num# Store Locations Daily Sales
1 UK $100
2 USA $150
3 UK $130
4 UAE $160

so, if I select UK from the dropdownbox,
it should return a value of $230
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this with a Label.
Note: Set the name of the sheet

VBA Code:
Private Sub ComboBox1_Change()
  Label1.Caption = ""
  With ComboBox1
    If .ListIndex > -1 Then Label1.Caption = .List(.ListIndex, 1)
  End With
End Sub

Private Sub UserForm_Activate()
  Dim dic As Object
  Dim c As Range
  Set dic = CreateObject("Scripting.Dictionary")
  With Sheets("StoreRecords")     'sheet name
    For Each c In .Range("B2", .Range("B" & Rows.Count).End(3))
      If c.Value <> "" Then
        dic(c.Value) = dic(c.Value) + c.Offset(, 1).Value
      End If
    Next
  End With
  ComboBox1.List = Application.Transpose(Array(dic.keys, dic.items))
  Label1.Caption = ""
End Sub

Example:

varios 29sep2022.xlsm
ABC
1NumStore LocationsDaily Sales
21UK100
32USA150
43UK130
54UAE160
StoreRecords


NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Solution
Try this with a Label.
Note: Set the name of the sheet

VBA Code:
Private Sub ComboBox1_Change()
  Label1.Caption = ""
  With ComboBox1
    If .ListIndex > -1 Then Label1.Caption = .List(.ListIndex, 1)
  End With
End Sub

Private Sub UserForm_Activate()
  Dim dic As Object
  Dim c As Range
  Set dic = CreateObject("Scripting.Dictionary")
  With Sheets("StoreRecords")     'sheet name
    For Each c In .Range("B2", .Range("B" & Rows.Count).End(3))
      If c.Value <> "" Then
        dic(c.Value) = dic(c.Value) + c.Offset(, 1).Value
      End If
    Next
  End With
  ComboBox1.List = Application.Transpose(Array(dic.keys, dic.items))
  Label1.Caption = ""
End Sub

Example:

varios 29sep2022.xlsm
ABC
1NumStore LocationsDaily Sales
21UK100
32USA150
43UK130
54UAE160
StoreRecords


NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
thank you for the quick response. much appreciated.

just a followup question. if my daily sales column is on a different column, say Column F, which part of the code should I change?
 
Upvote 0
This line:
VBA Code:
dic(c.Value) = dic(c.Value) + c.Offset(, 4)
 
Upvote 0
Im glad to help you. Thanks for the feedback
Hi, i have a follow up noob question again if you dont mind.

as I have not considered a case in which i only have ONE store location.
which part of the code would i have to adjust to accept the total sales for one store?
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top