列值未被正确的数据替换

时间:2017-11-13 22:23:35

标签: excel excel-vba vba

我编写了一个代码,用于检查D列中的每个单元格,并相应地将该值替换为逻辑。我已经完成了逻辑,但无法弄清楚错误。

enter image description here

注意:复制在C列的D列中粘贴相同的数据

 Sub Replace_country()

  Dim val As String
  Dim c As Range
  Dim rng As Range

  Set rng = ActiveSheet.Range("D2:D" & ActiveSheet.Range("D" & Rows.Count).End(xlUp).Row)

  For Each c In rng
    val = c.Value

    If val = "US" Or val = "USA" Or val = "United States" Then
      c.Value = "USA"

    ElseIf val = "India" Or val = "In" Or val = "ind" Then  //Error occurs here
      c.Value = "India"

    Else
      c.Value = "ROW"

    End If
  Next c
 End Sub

0 个答案:

没有答案