查找,匹配,然后在选项卡上相乘并替换活动单元格

时间:2016-12-19 00:15:07

标签: vba excel-vba lookup excel

好吧,我今天刚刚开始使用VBA /宏,我很高兴我的进展到目前为止:)但是我认为在我的工资等级之上打了一堵砖墙......

不寻求救助我想了解未来。我的工作真的可以使用这些......

名为“报价工具”的数据标签:

data tab

更新了CURRN TAB 然后我有一个名为“Currencies”的货币转换标签(刷新的网络数据):

currencies tab

我想做什么:在数据标签上搜索列中的货币,找到“CNY”或“HKD”或其他货币。当它发现它我想要去相应的“MSR”列单元格并将该值乘以与右计算转换相对应的“currency”选项卡中的单元格,然后将结果放在数据选项卡上的该单元格中。

我一直在研究这个问题大约5个小时,来自各个线程的不同代码。对于我需要的东西,我看起来太基本了:

来自用户的更新代码

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

我实际上设法使用不同的查找和替换来跨工作簿运行宏,但这让我很难过。你的想法很感激!

1 个答案:

答案 0 :(得分:0)

首先,您必须:

  • 要么改变所有&#34;货币&#34;工作表列A货币将字符串命名为相应的首字母缩略词(例如:更改&#34;中国人民币&#34;到&#34; CNY&#34;,...)

  • 或更改所有&#34;报价工具&#34;表格栏L货币首字母缩略词与其相应的名称(例如:更改&#34; CNY&#34;至&#34;中国人民币&#34;,...)

我想前者会更好

然后你可以使用如下(注释)的代码:

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