Look up, match, then multiply across tabs and repl

2019-06-11 06:21发布

问题:

okay i just started using VBA/macros today and im pretty pleased with my progress so far :) but hit a brick wall i think above my pay grade...

not looking for a bailout i'd like to understand this for the future. My job could really use these....

Data tab called "Quotation Tool":

UPDATED CURRN TAB Then I have a currency conv tab called "Currencies" (refreshed web data):

What I want to do: search the column for currency on data tab, find "CNY" or "HKD" or another currency. when it finds it i want to go corresponding "MSR" column cell and multiply that value by the cell in the "currencies" tab corresponding with the right currecy conversion then put result in that cell on the data tab.

I have been working on this for about 5 hours peicing together different codes from threads all over. what i have looks too basic for what i need:

UPDATED CODE FROM USER

Sub CurrencyConvTwo()

Dim cell As Range, currRng As Range, currCell As Range
With Worksheets("Currencies") '<--| reference "Currencies" sheet
    Set currRng = .Range("A3", .Cells(.Rows.Count, 1).End(xlUp)) '<--| set the range with all currencies acronyms
End With

With Worksheets("Quotation Tool") '<--| reference "Quotation Tool" sheet
    For Each cell In .Range("L3", .Cells(.Rows.Count, "L").End(xlUp)) '<--| loop through its column L ("Currency") cells from row 3 down to last not empty one
        Set currCell = currRng.Find(what:=cell.Value, LookIn:=xlValues, lookat:=xlWhole) '<--| try finding current currency in "Currencies" sheet currencies range
        If Not currCell Is Nothing Then cell.Offset(, 3) = cell.Offset(, 3) * currCell.Offset(, 3) '<--| if found, substitute current cell three columns offset to its current value times "Currencies" sheet found currency cell 2 columns offset
    Next cell
End With

End Sub

I actually managed to run macros across workbooks using different lookups and replacements but this is stumping me. your thoughts are appreciated!

回答1:

First off, you have to:

  • either change all "Currencies" sheet column A currencies names strings to their corresponding acronyms (e.g.: change "Chinese Yuan Renmimbi" to "CNY", ...)

  • or change all "Quotation Tool" sheet column L currency acronyms to their corresponding names (e.g.: change "CNY" to "Chinese Yuan Renmimbi" , ...)

I guess the former would be preferable

And then you can use a code like the following (commented) one:

Option Explicit

Sub CurrencyConv()
    Dim cell As Range, currRng As Range, currCell As Range

    With Worksheets("Currencies") '<--| reference "Currencies" sheet
        Set currRng = .Range("A3", .Cells(.Rows.count, 1).End(xlUp)) '<--| set the range with all currencies acronyms
    End With

    With Worksheets("Quotation Tool") '<--| reference "Quotation Tool" sheet
        For Each cell In .Range("L3", .Cells(.Rows.count, "L").End(xlUp)) '<--| loop through its column L ("Currency") cells from row 3 down to last not empty one
            Set currCell = currRng.Find(what:=cell.Value, LookIn:=xlValues, lookat:=xlWhole) '<--| try finding current currency in "Currencies" sheet currencies range
            If Not currCell Is Nothing Then cell.Offset(, 3) = cell.Offset(, 3) * currCell.Offset(, 2) '<--| if found, substitute current cell three columns offset to its current value times "Currencies" sheet found currency cell 2 columns offset
        Next cell
    End With
End Sub