如何在我的工作簿的其余部分之间转换2种货币USD和AED @rate 3.68,但仅限于每张纸上的选定单元格区域?

问题描述 投票:-2回答:2

例如。我希望我的介绍表“主表”可以选择在不同货币之间切换工作簿。美元和AED的汇率为3.68。有些单元格正在引用不同工作表中的其他单元格,因此我不想更改单元格引用,我只需要计算每个工作表中特定单元格的速率。

如何使用复选框或按钮完成此操作,以便从一开始就轻松转换。我正在使用excel for Mac。谢谢

excel vba excel-formula currency
2个回答
0
投票

我将假设您想要在输入单元格上进行转换,并且并非所有单元格都是公式,并且您要转换的许多单元格都是值。您应该认真考虑将输入与显示分开的答案,它将更加万无一失,并且可以防止任何可能破坏您的工作簿的逻辑。

如果你热衷于这个问题,那么请执行以下操作,但是,在你做之前......备份你的工作簿。我用以下代码完成的任何测试都没有破坏,但我没有你的工作簿,因此,我不保证。

首先,您需要一个能够为您提供当前汇率的单元格。您需要为该单元格指定一个命名范围的ExchangeRate。

在我的工作簿中,该单元格包含一个公式......

=IF(B1="USD",1,3.68)

看起来像这样......

enter image description here

...并且单元格B1附加了一个验证,允许您从2种货币,AED或USD中进行选择。

您说您希望能够确保只转换选定的单元格。为了确保我们只围绕那些单元格围栏,你需要在包含所有这些单元格的每个表格上创建一个命名范围。

该范围的名称需要称为CellsToConvert,您可以通过名称管理器执行此操作。创建命名范围时,请确保指定要为其创建的工作表,不要选择“工作簿”选项。

enter image description here

......下面显示了我在第一张纸上使用的零星范围。所有有色细胞都是该范围的一部分。绿色单元格包含值,黄色单元格包含公式。

enter image description here

在一天结束时,该范围可能是巨大的,并且在不同的工作表上,但它应该工作。

现在,将以下代码添加到VBA编辑器中的ThisWorkbook对象中...

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim objCell As Range, dblExRate As Double, strFormula As String, objSheet As Worksheet
    Dim strNewFormula As String, strOpeningChar As String, bIsFormula As Boolean
    Dim objCells As Range, strError As String, strExRateRangeName As String

    strExRateRangeName = "ExchangeRate"

    dblExRate = Range(strExRateRangeName)

    Application.EnableEvents = False

    For Each objSheet In Worksheets
        On Error Resume Next

        strError = ""

        Err.Clear

        Set objCells = objSheet.Range("CellsToConvert")

        strError = Err.Description

        On Error GoTo 0

        If strError = "" Then
            For Each objCell In objCells
                strFormula = objCell.FormulaR1C1
                bIsFormula = False

                ' Check to make sure this field contains a formula.
                If Left(strFormula, 1) = "=" And objCell.NumberFormat <> "@" Then
                    bIsFormula = True
                End If

                If dblExRate = 1 Then
                    ' Base currency selected.
                    ' Check to see if the cell contains a formula, if it does,
                    ' convert it back to a value
                    If bIsFormula Then
                        ' It's a formula and the cell is not set to text, proces it back
                        ' to its original value, that could still be a formula.

                        ' Remove all of the exchange rate components we would've added as
                        ' a part of this routine.
                        strNewFormula = Replace(strFormula, ") * " & strExRateRangeName, "")

                        ' Check to see if the formula has changed against the previous statement,
                        ' if it has, then it contained the custom additions, otherwise, it didn't.
                        If strFormula <> strNewFormula Then
                            strNewFormula = Mid(strNewFormula, 3)

                            ' Check to see if the new value is numeric, if it is, remove the leading
                            ' equals sign as it wasn't originally a formula, or, at least it doesn't
                            ' need to be a formula.
                            If IsNumeric(strNewFormula) Then
                                objCell.Value = strNewFormula
                            Else
                                objCell.FormulaR1C1 = "=" & strNewFormula
                            End If
                        End If
                    End If
                Else
                    ' Something other than the base currency has been selected.
                    strNewFormula = objCell.FormulaR1C1

                    If InStr(1, strNewFormula, strExRateRangeName, vbTextCompare) = 0 Then
                        If bIsFormula Then strNewFormula = Mid(objCell.FormulaR1C1, 2)

                        objCell.FormulaR1C1 = "=(" & strNewFormula & ") * " & strExRateRangeName
                    End If
                End If
            Next
        End If
    Next

    Application.EnableEvents = True
End Sub

...一旦你完成了以上所有工作,它应该适合你。如果工作簿很大,则可以测试性能,但这是您需要自己检查的内容。

如果您更改单元格并且它在其中一个范围内并且未选择USD的货币,则在您按Enter键后,您将看到输入值已更改为公式。当你想到它时,它非常整洁但可能不适合你。

最后要注意的是,如果您的范围包含断开的链接,该工作表的计算将失败,我的代码将不会通知您。

这为您添加了另一个选项,但比第一个答案更具风险。没有什么比选择更好的了。 :-)


1
投票

创建具有验证下拉列表的单元格,允许在AED和USD之间进行选择。将该单元格转换为命名范围,以便在整个工作簿中轻松引用。您可以将其称为“Curr”,“货币”的缩写(简称因为它会经常使用)。

我建议您在输入速率的位置创建一个类似的单元格,当前为3.68但是计划仅更改该单元格中的速率并将其应用于所有工作簿。将该单元命名为“Rate”。

现在,包含您可能想要切换的值的所有单元格都将遵循以下公式。 =[CellValue] * IF(Curr = "AED", Rate, 1)。此公式假定值均以美元输入。如果将它们输入AED,公式应如下所示。 = ROUND([CellValue] / IF(Curr = "AED", 1, Rate), 2)

如您所见,此解决方案需要在某处记录原始单元格值,这意味着用于数据捕获的单元格不能与用于数据显示的单元格相同。如果您希望捕获并显示在同一单元格中,则需要使用代码进行转换。

从表面上看,这似乎很简单:当更改Curr选择时,将重新计算所有具有受影响值的单元格。在实践中,这将以灾难告终,因为有1001种方式可能会出现问题,然后您将丢失所有数据,而不知道当时值是USD还是AED。

因此,起点需要分开数据捕获和数据显示。一旦完成,工作表功能可能不仅是实现您想要的最简单而且最有效的方式。

© www.soinside.com 2019 - 2024. All rights reserved.