通过单击相同单元格中的按钮在两种货币之间转换

时间:2015-07-14 10:04:02

标签: excel vba excel-vba currency

我想在我的Excel工作簿中创建一个函数,通过单击按钮将相同货币(SLL)的货币格式转换为另一个(DKK)的三张工作表中的所有单元格(相同)按钮以SLL或DKK格式转换,具体取决于当前值所在的货币。

我的代码是:

Sub convertcurrency()

    Dim userrate1 As Long
    Dim userrate2 As Long

    For Each cell In ActiveWorkbook.Worksheets

        userrate1 = 625

        If Cells.NumberFormat = "DKK" & "$ #,##0.00" _
        Then cell.Value = "SLL" & userrate1 * cell.Value

        ElseIf Cells.NumberFormat = "SLL" & "$ #,##0.00" _
        Then cell.Value = "DKK" & (1 / userrate1) * cell.Value _

    End If

End Sub

但它不起作用。错误是“编译错误。否则没有”。但是如果我需要包含第二个限制,我怎么能不使用else。

2 个答案:

答案 0 :(得分:0)

Sub test()
    Dim userrate1 As Long
Dim userrate2 As Long

For Each cell In ActiveWorkbook.Worksheets

    userrate1 = 625

    If Cells.NumberFormat = "DKK" & "$ #,##0.00" _
    Then cell.Value = userrate1 * cell.Value

        If Cells.NumberFormat = "SLL" & "$ #,##0.00" _
        Then cell.Value = (1 / userrate1) * cell.Value _



End Sub

试试这个

答案 1 :(得分:0)

我不确定功能,但这会更正语法。

Sub convertCurrency()

    Dim userrate1 As Double, userrate2 As Double
    Dim cr As Range, currRng As Range, ws As Worksheet

    userrate1 = 625
    userrate2 = 1 / userrate1

    For Each ws In ActiveWorkbook.Worksheets
        With ws
            On Error Resume Next
            Set currRng = .Cells.SpecialCells(xlCellTypeConstants, xlNumbers)
            On Error GoTo 0
            If Not currRng Is Nothing Then
                For Each cr In currRng
                    If cr.NumberFormat = "DKK" & "$ #,##0.00" Then
                        cr.Value = "SLL" & userrate1 * cr.Value           '<-one of these should probably be userrate2
                    ElseIf cr.NumberFormat = "SLL" & "$ #,##0.00" Then
                        cr.Value = "DKK" & (1 / userrate1) * cr.Value     '<-one of these should probably be userrate2
                    Else
                        'send non-matching number format to the VBE's Immediate window (Ctrl+G) to see what was missed
                        debug.print cr.numberformat
                    End If
                Next cr
            End If

            On Error Resume Next
            Set currRng = .Cells.SpecialCells(xlCellTypeFormulas, xlNumbers)
            On Error GoTo 0
            If Not currRng Is Nothing Then
                For Each cr In currRng
                    If cr.NumberFormat = "DKK" & "$ #,##0.00" Then
                        cr.Value = "SLL" & userrate1 * cr.Value           '<-one of these should probably be userrate2
                    ElseIf cr.NumberFormat = "SLL" & "$ #,##0.00" Then
                        cr.Value = "DKK" & (1 / userrate1) * cr.Value     '<-one of these should probably be userrate2
                    Else
                        'send non-matching number format to the VBE's Immediate window (Ctrl+G) to see what was missed
                        debug.print cr.numberformat
                    End If
                Next cr
            End If
        End With
    Next ws

End Sub

我试图从原始问题和评论中的描述中尽可能地纠正您的逻辑。 Range.SpecialCells method只会查看不是由原始代码指示的公式生成的数字。