VBA: Using SUMIF on column H when value in column A is unique

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
364
Office Version
  1. 2019
I have exported sales data to work with that has order numbers in Column A and shipping costs in column H. Unfortunately the export includes the same shipping value on all lines of the order, despite just the one shipping charge being applied per order. For example:
Shopify to Xero.xlsm
ABCDEFGH
1Order #PaidDateSKUQuantityPriceCurrencyShipping
2#1094TRUE6/12/20231VER21DOCLD25AUD22
3#1094TRUE6/12/20231CAS17DOCLD35AUD22
4#1097TRUE13/12/20231TEM18DOCLD30AUD22
5#1097TRUE14/12/20231SOT19DOCLD40AUD22
6#1098TRUE15/12/20232VER21DOCLD25AUD22
MrExcel


Both Order #1094 and #1097 were only charged a single value of $22 shipping each. How can I get a sum of all the actual shipping charges in this range? The correct answer is 2 x $22 = $44.

Is there a way to sum column H where column A is unique?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Sorry, that's my mistake in supplying some 'random' data to work with. Excel's fill function filled a series instead of copy. It should be just #1094 and #1097 showing.
 
Upvote 0
I am not sure if vba is needed for this. Try to put this formula in H7:
Excel Formula:
=SUMPRODUCT(1/COUNTIF(A2:A6,A2:A6),(H2:H6))
 
Upvote 0
This is the last step in a macro to take data from a .csv file exported from Shopify and organise it into another .csv file that suits the Xero accounting software. I need to find the total shipping charged to customers for the period that the report covers and enter that into Xero. Hopefully without having to manually type in a single value.
 
Upvote 0
VBA Code:
Sub test()
  Dim totalShipping As Double, lRow As Long
  lRow = Cells(Rows.Count, "A").End(xlUp).Row
  totalShipping = Evaluate("=SUMPRODUCT(1/COUNTIF(A2:A" & lRow & ",A2:A" & lRow & "),(H2:H" & lRow & "))")
  MsgBox totalShipping
End Sub
 
Upvote 0
Solution
This solution has been working well. I've used it in other iterations too, up until today when I have encountered a situation where the column in question contains no data. Ideally the data I'm working with would contain "0" when there is no value to consider, but the data I receive is just blank in that column instead, so I get a Type Mismatch error. Is there a way to handle the odd case that there is no data to sum in the particular column, and so the variable will just be set to zero?
 
Upvote 0
Hi,

Are you asking something like this:
VBA Code:
totalShipping = Evaluate("=SUMPRODUCT(IFERROR(1/COUNTIF(A2:A" & lRow & ",A2:A" & lRow & "),0),(H2:H" & lRow & "))")
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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