Excel VBA脚本索引错误

时间:2016-03-04 02:08:34

标签: excel vba excel-vba

我现在正在寻找半小时的修复。我有3个可能的值(低,中和高),我希望d是从1到3的值。运行脚本时出错。

Sub GetStructure()

Dim d As Integer
Dim i As Integer
Dim z As Integer
Dim k As Integer
Dim o As Integer
Dim o2 As Integer

  i = Worksheets("Control").Cells(14, 2).Value
  d = Worksheets("Control").Cells(9, 2).Value

  If d = "High" Then
  d = 3
  End If

  If d = "Mid" Then
  d = 2
  End If

  If d = "Low" Then
  d = 1

  End If

1 个答案:

答案 0 :(得分:3)

您定义Dim d As Integer

整数是一个数字。如果您编写If d = "High",则将整数与字符串

进行比较

因此,定义另一个变量Dim v As String,然后您可以指定v = Worksheets("Control").Cells(9, 2).Value并在If v = "High" Then处查看所有案例。