声明要在宏公式/格式中使用的变量的入门

时间:2019-06-09 08:49:12

标签: excel vba

每个Excel工作表只有一个货币代码变体。

据我了解,我将需要声明一个整数/字符串,并根据货币代码的不同而有所变化。 我不确定如何开始声明它。

这只是我到目前为止所了解的。

IF Table1[CurrencyCode] = USD or AUD or SGD or ...
THEN
x = 2 
y = "#,##0.00"

ELSE
x = 0
y = "#,##0"

将要使用变量的相关公式

ActiveCell.Formula = "=ROUND((R[-1]C[3]*RC[-1]),x)"
Selection.NumberFormat = "y"

2 个答案:

答案 0 :(得分:0)

我认为您的代码中存在一些与字符串连接和变量使用有关的错误

您可以改用此行吗?

ActiveCell.Formula = "=ROUND((R[-1]C[3]*RC[-1])," & x & ")"
Selection.NumberFormat = y

答案 1 :(得分:0)

如果使用"x"(用引号引起来),则是告诉VBA您想要一个包含单个字符"x"的文字字符串。

如果删除引号,则表示VBA想要变量x的当前值是什么。

在下面的代码中留下了一些注释,希望可以进一步帮助/解释。

Option Explicit

Private Sub FormatSomeCurrency()

    ' Declare your variables to use them later
    Dim roundingPrecision As Long
    Dim numberFormatToApply As String

    ' How are you going to get the currency code from the/each sheet?
    ' This is a task/question in itself and is not clear from your question.
    ' This code below is basically getting the value from "CurrencyCode" column
    ' of the table "Table1"
    ' The (1) at the end means we only want the first value (in the column).
    ' Change this as necessary.
    Dim currencyCodeFromSheet As String
    currencyCodeFromSheet = ThisWorkbook.Worksheets("Sheet1").Range("Table1[CurrencyCode]")(1)

    Dim specialCurrencies As Variant ' These currencies are "special" in the context of this procedure in that they require specific treatment
    specialCurrencies = Array("USD", "AUD", "SGD") ' You could also (instead) get this array from a worksheet instead of hard coding it here.

    ' This next line is pretty much equivalent to:
    '       IF Table1[CurrencyCode] = USD or AUD or SGD or ...
    If IsNumeric(Application.Match(currencyCodeFromSheet, specialCurrencies, 0)) Then
        roundingPrecision = 2
        numberFormatToApply = "#,##0.00"
    Else
        roundingPrecision = 0
        numberFormatToApply = "#,##0"
    End If

    ' If you're expecting ActiveCell to be cell A1 (for example), then in your code say Range("A1") instead of ActiveCell
    ' Better yet, specify the workbook and worksheet that cell A1 belongs to. The more specific you are, the better.
    With ActiveCell
        .FormulaR1C1 = "=ROUND((R[-1]C[3]*RC[-1])," & roundingPrecision & ")"
        .NumberFormat = numberFormatToApply
    End With

End Sub

重构为接受参数的过程

Option Explicit

Private Sub FormatSomeCurrencyCell(ByRef cellToFormat As Range)

    Dim currencyCodeFromSheet As String
    currencyCodeFromSheet = ThisWorkbook.Worksheets("Sheet1").Range("Table1[CurrencyCode]")(1)

    Dim specialCurrencies As Variant ' Require specific treatment
    specialCurrencies = Array("USD", "AUD", "SGD")

    Dim roundingPrecision As Long
    Dim numberFormatToApply As String

    If IsNumeric(Application.Match(currencyCodeFromSheet, specialCurrencies, 0)) Then
        roundingPrecision = 2
        numberFormatToApply = "#,##0.00"
    Else
        roundingPrecision = 0
        numberFormatToApply = "#,##0"
    End If

    With cellToFormat
        .FormulaR1C1 = "=ROUND((R[-1]C[3]*RC[-1])," & roundingPrecision & ")"
        .NumberFormat = numberFormatToApply
        .Interior.Color = vbYellow
    End With

End Sub

Private Sub InvokeProcedure() ' <-- Call this whatever you want.

    ' Continuing on from the initial example
    FormatSomeCurrencyCell ActiveCell

    ' You could also call it as below (for example):
    FormatSomeCurrencyCell ThisWorkbook.Worksheets("Sheet1").Range("B10")

    ' Or on multiple cells at once:
    FormatSomeCurrencyCell ThisWorkbook.Worksheets("Sheet1").Range("B10:B20")
End Sub