简体   繁体   English

如何在整个工作簿的其余部分但仅在每张工作表上的选定单元格范围内在 2 种货币 USD 和 AED @rate 3.68 之间进行转换?

[英]How can I convert between 2 currencies USD and AED @rate of 3.68 throughout the rest of my workbook but only in selected cell ranges on each sheet?

For example.例如。 I want my intro sheet "Main Sheet" to have an option to switch the workbook between currencies.我希望我的介绍表“主表”可以选择在货币之间切换工作簿。 USD and AED at the rate of 3.68.美元和迪拉姆的汇率为 3.68。 Some cells are referencing other cells in different sheets, so I don't want to change the cell references, I only need to calculate the rate in specific cells within each sheet.有些单元格引用了不同工作表中的其他单元格,所以我不想更改单元格引用,我只需要计算每个工作表中特定单元格的比率。

How can I accomplish this preferably using a check box or button for easy converting from the start.我如何最好使用复选框或按钮来完成此操作,以便从一开始就轻松转换。 I'm using excel for Mac.我在 Mac 上使用 excel。 Thank you谢谢

Create a cell with a validation drop-down allowing to choose between AED and USD.创建一个带有验证下拉列表的单元格,允许在 AED 和 USD 之间进行选择。 Convert that cell to a named range for easy referencing throughout the workbook.将该单元格转换为命名范围,以便在整个工作簿中轻松引用。 You might call it "Curr", short for "Currency" (short because it will be used often).您可以将其称为“Curr”,“Currency”的缩写(简称,因为它会经常使用)。

I recommend that you create a similar cell somewhere where you enter the rate, currently 3.68 but plan on changing the rate in that cell only and have it applied to all the workbook.我建议您在输入汇率的某处创建一个类似的单元格,当前为 3.68,但计划仅更改该单元格中的汇率并将其应用于所有工作簿。 Name that cell as "Rate".将该单元格命名为“Rate”。

Now all cells containing values which you may want switched would be subject to the following formula.现在,所有包含您可能想要切换的值的单元格都将遵循以下公式。 =[CellValue] * IF(Curr = "AED", Rate, 1) . =[CellValue] * IF(Curr = "AED", Rate, 1) This formula presumes that the values are all entered in USD.此公式假定所有值都以美元输入。 If they are entered in AED the formula should look as follows.如果它们是在 AED 中输入的,则公式应如下所示。 = ROUND([CellValue] / IF(Curr = "AED", 1, Rate), 2)

As you see, this solution would require the original cell values to be recorded somewhere, meaning, the cells used for data capture can't be the same as the ones used for data display.如您所见,此解决方案需要将原始单元格值记录在某处,这意味着用于数据捕获的单元格不能与用于数据显示的单元格相同。 If you wish to insist on capture and display being in the same cell you would need code to do the conversion.如果您想坚持在同一个单元格中捕获和显示,您将需要代码来进行转换。

On the face of it this seems simple: When the Curr selection is changed, all cells with affected values are re-calculated.从表面Curr ,这似乎很简单:当Curr选择更改时,所有具有受影响值的单元格都会重新计算。 In practise this would end in disaster because there are 1001 ways in which something might go wrong and then you would lose all your data, not knowing whether the values are USD or AED at that moment.在实践中,这将以灾难告终,因为有 1001 种方式可能会出错,然后您将丢失所有数据,不知道当时值是 USD 还是 AED。

Therefore the starting point needs to be to separate data capture and data display.因此,起点需要将数据捕获和数据显示分开。 Once that is done workheet functions might well be not only the easiest but also the most efficient way of achieving what you want.一旦完成,工作表函数可能不仅是实现您想要的最简单而且最有效的方法。

I'm going to assume that you want to have the conversion on the input cell and not all of your cells are formulas and that a lot of the cells you want to convert are values.我将假设您希望在输入单元格上进行转换,而不是所有单元格都是公式,并且您要转换的许多单元格都是值。 You should seriously consider the answer to split out input vs display, it will be much more foolproof and protected from any logic that may break your workbook.您应该认真考虑将输入与显示分开的答案,这将更加万无一失,并且可以防止任何可能破坏您的工作簿的逻辑。

If you're keen on this pathe then do the following, but, before you do ... BACKUP YOUR WORKBOOK .如果您热衷于这种方法,请执行以下操作,但是,在此之前...备份您的工作簿 Any tests I've done with the below code are not breaking but I don't have your workbook, therefore, I make no guarantees.我用下面的代码完成的任何测试都没有中断,但我没有你的工作簿,因此,我不做任何保证。

Firstly, you need a cell that gives you the current exchange rate.首先,您需要一个为您提供当前汇率的单元格。 You need to give that cell a named range of ExchangeRate .您需要为该单元格指定ExchangeRate的命名范围。

In my workbook, that cell contains a formula ...在我的工作簿中,该单元格包含一个公式...

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

It looks like this ...看起来像这样...

在此处输入图片说明

... and cell B1 has a validation attached to it that allows you to select from 2 currencies, AED or USD . ...并且单元格 B1 附加了一个验证,允许您从 2 种货币中进行选择, AEDUSD

You said you want to be able to ensure that only a selection of cells will be converted.您说过您希望能够确保仅转换选定的单元格。 To make sure we ring fence just those cells, you need to create a named range ON EACH SHEET that includes all of those cells.为了确保我们只对这些单元格进行围栏,您需要在每个工作表上创建一个包含所有这些单元格的命名范围。

The name of that range needs to be called CellsToConvert and you can do that through the Name Manager.该范围的名称需要称为CellsToConvert ,您可以通过名称管理器执行此操作。 When creating the named range, make sure you specify the worksheet you're creating it for, do not selected the "Workbook" option.创建命名范围时,请确保指定要为其创建的工作表,不要选择“工作簿”选项。

在此处输入图片说明

... the below shows the sporadic range I used on the first sheet. ... 下面显示了我在第一张纸上使用的零星范围。 All coloured cells a part of that range.所有彩色单元格都是该范围的一部分。 The green cells contain values and the yellow cells contain formulas.绿色单元格包含值,黄色单元格包含公式。

在此处输入图片说明

At the end of the day, that range can be huge and across different sheets but it should work.归根结底,该范围可能很大并且跨越不同的工作表,但它应该有效。

Now, add the following code into the ThisWorkbook object within the VBA editor ...现在,将以下代码添加到 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

... once you've done all of the above, it should work for you. ...一旦您完成了上述所有操作,它应该对您有用。 Performance could be tested if the workbook is large but that's something you'll need to check for yourself.如果工作簿很大,可以测试性能,但这需要您自己检查。

If you change a cell and it's within one of those ranges AND the currency of USD is not selected, you'll see the input value changed to a formula after you hit enter.如果您更改一个单元格并且它在这些范围之一内并且未选择美元货币,则在您按 Enter 键后,您将看到输入值更改为公式。 That's pretty neat when you think about it but may not be for you.当您考虑它时,这非常整洁,但可能不适合您。

One last thing to note, if your range contains broken links, the calculation for that sheet will fail and my code will not notify you of that.最后要注意的是,如果您的范围包含断开的链接,则该工作表的计算将失败,我的代码不会通知您。

This adds another option for you but is riskier than the first answer.这为您增加了另一个选择,但比第一个答案风险更大。 There's nothing like options.没有什么比选项。 :-) :-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的工作簿中的每张纸上运行这个 VBA 代码? - How can I run this VBA code across each sheet in my workbook? 如何确定函数所在的工作簿中的工作表名称? - How can I determine the sheet name in a workbook that my function is sitting? 使用 VBA,我可以从主工作表的单元格中的选定数字替换同一工作簿中另一个工作表中的范围 - With a VBA can I substitute a Range from another sheet in same workbook from a selected number in a cell from primary sheet 如果工作簿和工作表处于非活动状态,则无法使用范围 - Can't work with ranges if workbook and sheet are not active 如何在不同的单元格范围内将具有不同值的整行复制并粘贴到新表中? - How can I copy & paste entire rows with distinct values to a new sheet on varying cell ranges? 我如何通过其他工作簿中的活动单元格值来引用工作表 - How can i reffer sheet by active cell value from other workbook 将多个 Excel 工作表范围作为图片转换为新的 Excel 工作簿作为工作表 - Convert Multiple Excel Sheet Ranges as Picture to New Excel Workbook as worksheets 是否可以在不激活工作表的情况下将工作表中的单元格设置为选定的单元格? - Can I set a cell in a sheet as the selected one, without activating the sheet? 间接工作表名称仅作为变量,而不是工作簿或单元格 - Indirect Sheet name only as a variable, not workbook or cell 如何遍历 Excel 工作簿中的工作表并转置每张工作表中的数据? - How can I loop over sheets in an Excel workbook and transpose the data in each sheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM