简体   繁体   English

Excel宏抛出错误

[英]Excel Macro throwing errors

I have coded a macro that appears to work (on some worksheets), but it seems rather heavy to me. 我编写了一个似乎有效的宏(在一些工作表上),但对我来说似乎相当沉重。 Can you please take a look and see if changes can be made or a completely different macro that completes the same task. 您能否看看是否可以进行更改或完成相同任务的完全不同的宏。 I am looking for a value "EUR" or "USD" in a number of specified columns. 我在许多指定的列中寻找值“EUR”或“USD”。 When either of the two values are found, the macro performs a calculation on adjacent cells and then changes the value of the original cell to AED. 当找到两个值中的任何一个时,宏对相邻单元执行计算,然后将原始单元的值更改为AED。

Option Explicit

Sub Change_currency()
Const EUR_to_AED = 4.9
Const USD_to_AED = 3.64
  Dim R As Long
  Dim V1
  Dim V2
  Dim MyValue
  Dim MyValue2

  'Start at row 5
  R = 5

  While Cells(R, "K").Value <> ""
    If Cells(R, "K").Value = "EUR" Then
        Cells(R, "K").Activate
        MyValue = 4.9
        V1 = ActiveCell.Offset(0, -2).Value
        V2 = ActiveCell.Offset(0, -3).Value
        ActiveCell.Offset(0, -2).Value = MyValue * V1
        ActiveCell.Offset(0, -1).Value = V1 * EUR_to_AED * V2
        ActiveCell.Value = "AED"

    End If

While Cells(R, "K").Value <> ""
    If Cells(R, "K").Value = "USD" Then
        Cells(R, "K").Activate
        MyValue = 3.64
        V1 = ActiveCell.Offset(0, -2).Value
        V2 = ActiveCell.Offset(0, -3).Value
        ActiveCell.Offset(0, -2).Value = MyValue2 * V1
        ActiveCell.Offset(0, -1).Value = V1 * USD_to_AED * V2
        ActiveCell.Value = "AED"

End If

    'Next row
    R = R + 1
  Wend

End Sub

I am running into problems with some of the data on the sheets are not numerical and the macro throws an error. 我遇到问题,工作表上的一些数据不是数字和宏抛出错误。 So how to ignore those errors? 那么如何忽略这些错误呢?

Any help would be appreciated... 任何帮助,将不胜感激...

I usually prefer a For...Next loop to WhileWend, and you seem to be doing the exact same thing twice, depending on the currency, so I would simplify it: 我通常更喜欢ForWend的For ... Next循环,你似乎做了两次完全相同的事情,这取决于货币,所以我会简化它:

Sub Change_currency()

    Const EUR_to_AED          As Double = 4.9
    Const USD_to_AED          As Double = 3.64

    Dim wks                   As Worksheet
    Dim lngRow                As Long
    Dim lngNumRows            As Long
    Dim c                     As Range
    Dim dblConversion         As Double

    Dim V1                    As String
    Dim V2                    As String

    Set wks = ActiveSheet

    With wks

        ' this gets the last row number in column 11 that has a value
        lngNumRows = .Cells(.Cells.Rows.Count, 11).End(xlUp).Row

        For lngRow = 5 To lngNumRows

            Set c = .Cells(lngRow, 11)

            Select Case c.Value
                Case "EUR"
                    dblConversion = EUR_to_AED
                Case "USD"
                    dblConversion = USD_to_AED
            End Select

            V1 = c.Offset(0, -2).Value
            V2 = c.Offset(0, -3).Value

            If IsNumeric(V1) And IsNumeric(V2) Then
                c.Offset(0, -2).Value = V1 * dblConversion
                c.Offset(0, -1).Value = V1 * dblConversion * V2
                c.Value = "AED"
            End If

        Next lngRow

    End With

End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM