Excel 多个变量根据条件相乘会产生错误
我已经搜索了这个网站的所有内容,但是没有找到一个适合我需要的解决方案,我是excel的专家用户,但是我开始学习VBA,并且陷入了这个错误好几天了。 我需要将一个值乘以一种货币,由于我有很多货币,我认为这是正确的方法,请参阅下面的代码,但我遇到了无法解决的错误,有人能帮我解决吗? 提前谢谢Excel 多个变量根据条件相乘会产生错误,excel,vba,Excel,Vba,我已经搜索了这个网站的所有内容,但是没有找到一个适合我需要的解决方案,我是excel的专家用户,但是我开始学习VBA,并且陷入了这个错误好几天了。 我需要将一个值乘以一种货币,由于我有很多货币,我认为这是正确的方法,请参阅下面的代码,但我遇到了无法解决的错误,有人能帮我解决吗? 提前谢谢 Sub Update_Janeiro() Dim lngLastRow As Long Dim Cambio_Jan As Currency If Cells(i, 251) = EUR Then
Sub Update_Janeiro()
Dim lngLastRow As Long
Dim Cambio_Jan As Currency
If Cells(i, 251) = EUR Then
Cambio_Jan = 1
ElseIf Cells(i, 251) = USD Then
Cambio_Jan = 0.801228726
ElseIf Cells(i, 251) = GBP Then
Cambio_Jan = 1.1414211803
ElseIf Cells(i, 251) = CNY Then
Cambio_Jan = 0.1271895307
ElseIf Cells(i, 251) = NAIRAS Then
Cambio_Jan = 0.0016670478
ElseIf Cells(i, 251) = AUD Then
Cambio_Jan = 0.6431760061
ElseIf Cells(i, 251) = GHS Then
Cambio_Jan = 0.1778598686
ElseIf Cells(i, 251) = CZK Then
Cambio_Jan = 0.0397256232
ElseIf Cells(i, 251) = KES Then
Cambio_Jan = 0.0078621931
ElseIf Cells(i, 251) = ZAR Then
Cambio_Jan = 0.0676563785
ElseIf Cells(i, 251) = ZMK Then
Cambio_Jan = 0.0000829218
ElseIf Cells(i, 251) = TZS Then
Cambio_Jan = 0.0003555818
ElseIf Cells(i, 251) = SGD Then
Cambio_Jan = 0.6117066233
ElseIf Cells(i, 251) = UGX Then
Cambio_Jan = 0.000221247
ElseIf Cells(i, 251) = RON Then
Cambio_Jan = 0.2149924803
ElseIf Cells(i, 251) = RUB Then
Cambio_Jan = 0.0141866904
Else: Cambio_Jan = 0
End If
lngLastRow = Cells(Rows.Count, "IA").End(xlUp).Row
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
For i = 3 To lngLastRow
Cells(i, 261).Value = Cells(i, 255) + Cells(i, 256)
Cells(i, 262).Value = Cells(i, 259) * Cambio_Jan
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
更新代码
Option Explicit
Sub Update_Janeiro()
Dim lngLastRow As Long
Dim Cambio_Jan As Double
Dim i As Integer
i = i
If Cells(i, 251) = "EUR" Then
Cambio_Jan = 1
ElseIf Cells(i, 251) = "USD" Then
Cambio_Jan = 0.801228726
ElseIf Cells(i, 251) = "GBP" Then
Cambio_Jan = 1.1414211803
ElseIf Cells(i, 251) = "CNY" Then
Cambio_Jan = 0.1271895307
ElseIf Cells(i, 251) = "NAIRAS" Then
Cambio_Jan = 0.0016670478
ElseIf Cells(i, 251) = "AUD" Then
Cambio_Jan = 0.6431760061
ElseIf Cells(i, 251) = "GHS" Then
Cambio_Jan = 0.1778598686
ElseIf Cells(i, 251) = "CZK" Then
Cambio_Jan = 0.0397256232
ElseIf Cells(i, 251) = "KES" Then
Cambio_Jan = 0.0078621931
ElseIf Cells(i, 251) = "ZAR" Then
Cambio_Jan = 0.0676563785
ElseIf Cells(i, 251) = "ZMK" Then
Cambio_Jan = 0.0000829218
ElseIf Cells(i, 251) = "TZS" Then
Cambio_Jan = 0.0003555818
ElseIf Cells(i, 251) = "SGD" Then
Cambio_Jan = 0.6117066233
ElseIf Cells(i, 251) = "UGX" Then
Cambio_Jan = 0.000221247
ElseIf Cells(i, 251) = "RON" Then
Cambio_Jan = 0.2149924803
ElseIf Cells(i, 251) = "RUB" Then
Cambio_Jan = 0.0141866904
Else: Cambio_Jan = 0
End If
lngLastRow = Cells(Rows.Count, "IA").End(xlUp).Row
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
For i = 3 To lngLastRow
Cells(i, 261).Value = Cells(i, 255) + Cells(i, 256)
Cells(i, 262).Value = Cells(i, 259) * Cambio_Jan
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
注意,您可以使用selectcase
如果使用Cellsi,251=EUR,EUR是一个变量,而不是字符串。
您需要使用quotes Cellsi,251=欧元
您的i未定义,并且没有第0行。定义i或与i一起使用循环
无论如何,我不建议将这些值存储在代码中。在额外的工作表中提供它们,以便轻松更新它们
使用OptionExplicit并正确声明所有变量
您使用了货币类型,请参见
货币变量以整数格式存储为64位8字节数字,按10000缩放,得到一个固定点数,小数点左侧为15位,右侧为4位
但您使用的数字超过4位。换成双字
所以你最终会得到这样的结果:
Option Explicit
Sub Update_Janeiro()
Dim lngLastRow As Long
lngLastRow = Cells(Rows.Count, "IA").End(xlUp).Row
Dim Cambio_Jan As Double
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim i As Long 'must be long!
For i = 3 To lngLastRow
Select Case Cells(i, 251)
Case "EUR": Cambio_Jan = 1
Case "USD": Cambio_Jan = 0.801228726
Case "GBP": Cambio_Jan = 1.1414211803
Case "CNY": Cambio_Jan = 0.1271895307
Case "NAIRAS": Cambio_Jan = 0.0016670478
Case "AUD": Cambio_Jan = 0.6431760061
Case "GHS": Cambio_Jan = 0.1778598686
Case "CZK": Cambio_Jan = 0.1778598686
Case "KES": Cambio_Jan = 0.0078621931
Case "ZAR": Cambio_Jan = 0.0676563785
Case "ZMK": Cambio_Jan = 0.0000829218
Case "TZS": Cambio_Jan = 0.0003555818
Case "SGD": Cambio_Jan = 0.6117066233
Case "UGX": Cambio_Jan = 0.000221247
Case "RON": Cambio_Jan = 0.2149924803
Case "RUB": Cambio_Jan = 0.0141866904
Case Else: Cambio_Jan = 0
End Select
Cells(i, 261).Value = Cells(i, 255) + Cells(i, 256)
Cells(i, 262).Value = Cells(i, 259) * Cambio_Jan
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
我遇到了这个我无法解决的错误-错误是什么?它发生在哪一行?代码开头的I值是多少?当你点击If Cellsi,251=EUR,然后行,看起来我将等于0。如果您的代码顶部有Option Explicit,您应该会得到变量not defined error(未定义错误)。错误是运行时错误1004,发生在以下代码之后:Sub Update_Janeiro Dim lngLastRow只要Dim Cambio_Jan作为货币,如果Cellsi,251=欧元,那么Cambio_Jan=1i=i没有意义,我需要设置为数值!或者您需要将所有i移动到For i循环中。我将i更改为3,因为我希望根据单元格251中的currenci代码在第259列中从第3行开始直到最后一行的每一行中进行更改,但现在代码没有给出错误,它只是不做任何事情…进行了所有更改,包括“dim i as integer”,但它在代码的相同位置给出了相同的错误。有什么想法吗?将更新后的代码添加到当前代码下方的原始问题中,以便我们可以看到差异。请参阅我的编辑。这可能行得通。您的i必须在For i循环中。否则它不算i,它是0。非常感谢PEH,这起作用了:你是一个生命saver@P.Costa我仍然建议将所有cambio值写入工作表,而不是将它们写入代码中。那么更新起来就容易多了。
Option Explicit
Sub Update_Janeiro()
Dim lngLastRow As Long
lngLastRow = Cells(Rows.Count, "IA").End(xlUp).Row
Dim Cambio_Jan As Double
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim i As Long 'must be long!
For i = 3 To lngLastRow
Select Case Cells(i, 251)
Case "EUR": Cambio_Jan = 1
Case "USD": Cambio_Jan = 0.801228726
Case "GBP": Cambio_Jan = 1.1414211803
Case "CNY": Cambio_Jan = 0.1271895307
Case "NAIRAS": Cambio_Jan = 0.0016670478
Case "AUD": Cambio_Jan = 0.6431760061
Case "GHS": Cambio_Jan = 0.1778598686
Case "CZK": Cambio_Jan = 0.1778598686
Case "KES": Cambio_Jan = 0.0078621931
Case "ZAR": Cambio_Jan = 0.0676563785
Case "ZMK": Cambio_Jan = 0.0000829218
Case "TZS": Cambio_Jan = 0.0003555818
Case "SGD": Cambio_Jan = 0.6117066233
Case "UGX": Cambio_Jan = 0.000221247
Case "RON": Cambio_Jan = 0.2149924803
Case "RUB": Cambio_Jan = 0.0141866904
Case Else: Cambio_Jan = 0
End Select
Cells(i, 261).Value = Cells(i, 255) + Cells(i, 256)
Cells(i, 262).Value = Cells(i, 259) * Cambio_Jan
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub