• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

Putting shipping rates in rows (reading from table)

Hi,

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.
shipping-rates.xls
0
NickHoward
Asked:
NickHoward
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
Saqib Husain, SyedEngineerCommented:
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"))))
0
 
lwadwellCommented:
If you change the format of the shipping rates table ... a vlookup will work very easily.
shipping-rates.xls
0
 
NickHowardAuthor Commented:
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.

Nick
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Saqib Husain, SyedEngineerCommented:
If the first solution gives correct results then I can put it into a macro.
0
 
tdlewisCommented:
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.
0
 
NickHowardAuthor Commented:
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:

us=,gb=12,nl=10

should apear as
gb=12,nl=10

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

Thanks.
0
 
tdlewisCommented:
I have written a macro (below) that should do what you're asking, but I get different values for "es" than you suggested:
us=10,gb=8,es=10
us=14,gb=12,es=10
us=14,gb=12,es=10
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

0
 
tdlewisCommented:
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
0
 
NickHowardAuthor Commented:
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?

Thanks.
0
 
tdlewisCommented:
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?
shipping-rates.xls
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now