Using IF condition in VBA Dictionary

chriscorpion786

Board Regular
Joined
Apr 3, 2011
Messages
108
Office Version
  1. 365
Platform
  1. Windows
Hi,

Im using the VBA dictionary to make a summary report by salesrep for quantity and value. The problem is if I put an IF condition testing for the year to be 2017, one of the values comes wrong. If I remove the IF condition the code works fine and gives all the correct results. Is there a problem using an IF condition like below. How can i modify the code to test a condition.


Code:
For x = 2 To rng.Rows.Count


If sht.Cells(x, 1).value = 2017 Then


salesrep = sht.Cells(x, 4).value
qty = sht.Cells(x, 10).value
value = sht.Cells(x, 9).value


End If


dictqty(salesrep) = dictqty(salesrep) + qty
dictvalue(salesrep) = dictvalue(salesrep) + value


Next x




x = 2


For Each key In dictqty


Cells(x, 1).value = key
Cells(x, 2).value = dictqty(key)
Cells(x, 3).value = dictvalue(key)


x = x + 1


Next key

Rgds,

Mustafa
 
Last edited by a moderator:

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Maybe move the "End if" as below :-
Code:
For x = 2 To rng.Rows.Count
    If sht.Cells(x, 1).value = 2017 Then
         salesrep = sht.Cells(x, 4).value
        qty = sht.Cells(x, 10).value
        value = sht.Cells(x, 9).value

       dictqty(salesrep) = dictqty(salesrep) + qty
       dictvalue(salesrep) = dictvalue(salesrep) + value
  End If

Next x
 
Upvote 0
Maybe move the "End if" as below :-
Code:
For x = 2 To rng.Rows.Count
    If sht.Cells(x, 1).value = 2017 Then
         salesrep = sht.Cells(x, 4).value
        qty = sht.Cells(x, 10).value
        value = sht.Cells(x, 9).value

       dictqty(salesrep) = dictqty(salesrep) + qty
       dictvalue(salesrep) = dictvalue(salesrep) + value
  End If

Next x

Got it.....I should have thought of that...Code works now.
THANKS!!
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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