Putting shipping rates in rows (reading from table)

Posted on 2012-09-10
Medium Priority
Last Modified: 2012-09-15

I have two sheets, in one sheet is my "products" where I want to have shipping rates calculated from other sheet i.e. "Shipping rates" but displaying in rows against each product item.

Example (product sheet where "Tariff" being calculated from "Shipping rates":

Product          Weight      Tariff
product A          0.6      us=10,gb=8,es=8
product B          1.6      us=14,gb=12,es=12
product C           2      us=14,gb=12,es=10

While my "Shipping rates" contain following tariff table for each country:

wt_start       wt_end       Rate       Country
0       0.5       8       us
0.5       1       10       us
1       1.5       12       us
1.5       2       14       us
2       2.5       16       us
2.5       3       18       us
0       1       8       gb
1       1.5       10       gb
1.5      2      12      gb
2      2.5      13      gb
0      2.5      10      es
2.5      4.5      12      es
4.5      2      14      es

I need a macro so that "Tariff" column is calculated automtically based on Shipping rates for each product in product sheet.

I am attaching the file and appreciate if you can write code in it.

Many thanks.
Question by:NickHoward
  • 4
  • 3
  • 2
  • +1
LVL 43

Assisted Solution

by:Saqib Husain, Syed
Saqib Husain, Syed earned 600 total points
ID: 38382738
Does this have to be VBA? You can even use this formula in D2 and copy it down

="us="&INDEX('Shipping rates'!$C:$C,MATCH("us",'Shipping rates'!D:D,0)-1+MATCH(B2-0.001,OFFSET('Shipping rates'!$D$1,MATCH("us",'Shipping rates'!D:D,0)-1,-3,COUNTIF('Shipping rates'!D:D,"us"))))&",gb="&INDEX('Shipping rates'!$C:$C,MATCH("gb",'Shipping rates'!D:D,0)-1+MATCH(B2-0.001,OFFSET('Shipping rates'!$D$1,MATCH("gb",'Shipping rates'!D:D,0)-1,-3,COUNTIF('Shipping rates'!D:D,"gb"))))&",es="&INDEX('Shipping rates'!$C:$C,MATCH("es",'Shipping rates'!D:D,0)-1+MATCH(B2-0.001,OFFSET('Shipping rates'!$D$1,MATCH("es",'Shipping rates'!D:D,0)-1,-3,COUNTIF('Shipping rates'!D:D,"es"))))
LVL 25

Expert Comment

ID: 38382751
If you change the format of the shipping rates table ... a vlookup will work very easily.

Author Comment

ID: 38383148
Thanks for the solution.

I wish to have a macro as I have huge shipping rate table for 24 countries being loaded from database.

Hope someone can help in this direction.

I will share the points indeed.

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38383353
If the first solution gives correct results then I can put it into a macro.
LVL 10

Expert Comment

ID: 38383756
Is there a reason to have the Tariff information be in a single cell per product? Once you assemble that information for 24 countries, that cell will be very long and practically unusable by a human. Would it work to have 24 tariff columns (one per country) instead?

Note: It is relatively straightforward to write a macro to populate the information whether the desired result is one cell per product or multiple tariff cells.

Author Comment

ID: 38384081
You are correct - infact I am making a product feed in xml where tariff has to be on single line. They do support 255 characters though.

Only issue to be resolved when creating tariff is that if no tariff found then it should not include, for instance:


should apear as

Macro loop through all ISO country abbreviations and create the tariff accordingly.

LVL 10

Expert Comment

ID: 38385138
I have written a macro (below) that should do what you're asking, but I get different values for "es" than you suggested:
Sub ComputeTariffs()
Dim i As Long
Dim j As Long
Dim lastShipping As Long
Dim oProdSheet As Worksheet
Dim oShipSheet As Worksheet
Dim tariff As String

    Set oProdSheet = Sheets("product")
    Set oShipSheet = Sheets("Shipping rates")
    lastShipping = oShipSheet.Range("A" & oShipSheet.Rows.Count).End(xlUp).Row
    For i = 2 To oProdSheet.Range("A" & oProdSheet.Rows.Count).End(xlUp).Row
        tariff = ""
        For j = 2 To lastShipping
            If oShipSheet.Cells(j, 1).Value < oProdSheet.Cells(i, 2).Value And _
                oShipSheet.Cells(j, 2).Value >= oProdSheet.Cells(i, 2).Value Then
                If tariff <> "" Then tariff = tariff & ","
                tariff = tariff & oShipSheet.Cells(j, 4).Value & "=" & _
                    oShipSheet.Cells(j, 3).Value
            End If
        Next j
        If tariff <> "" Then
            oProdSheet.Cells(i, 4) = tariff
        End If
    Next i
End Sub

Open in new window

LVL 10

Expert Comment

ID: 38385476
One thing you didn't specify is what to do when the weight is matches multiple rows. Is a weight of 0.5 supposed to yield us=8 or us=10? The solution posted above assumes that us=8 is the correct value.

wt_start       wt_end       Rate       Country
0       0.5       8       us
0.5       1       10       us

Author Comment

ID: 38385851
Hi tdlewis,

It okay to have match lowest value like us=8 you suggested above.

I tried the macro but getting erors. Can you please check again? May be putting back in my earlier excel can help to have placed in correct order?

LVL 10

Accepted Solution

tdlewis earned 1400 total points
ID: 38387217
I just opened your original worksheet, pasted the code, and it runs without any errors. I've attached the updated Excel file.

When I was testing the macro, I put the result into column D so that I could see the macro results alongside your example. In order for the results to appear in column C this line:
    oProdSheet.Cells(i, 4) = tariff
will need to change to:
    oProdSheet.Cells(i, 3) = tariff

However, that does not explain why you're getting an error. What is the error message and on which line does it occur?

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question