Excel VBA - 运行-时间错误 438 - 使用 For 循环提取值时
Excel VBA - Run-time error 438 - When pulling a value using a For Loop
我正在尝试从 Sheet 中的一系列单元格中提取一个字符串,以在 If 语句中进行测试以确定要执行的操作。调试突出显示以下行作为错误原因:
testValue = Sheets("Issue Reference").testRange.Value
但我不明白为什么这是个问题,也许是 .Value 属性 的问题?有任何想法吗?完整代码如下:
Private Sub SubGroupList_Click()
Dim selection As String
Dim rng As Range
Dim testRange As Range
Dim i As Integer
Dim testValue As String
selection = SubGroupList.Value
If failedComponentTrack = 1 Then
Set rng = Range("C3:C10")
ElseIf failedComponentTrack = 2 Then
Set rng = Range("E3:E10")
ElseIf failedComponentTrack = 3 Then
Set rng = Range("G3:G10")
Else
End If
i = 1
For Each testRange In rng
testValue = Sheets("Issue Reference").testRange.Value
If selection = testValue Then
Sheets("DataStore").Cells(LB, 27).Value = selection
failedComponentTrack = i
End If
i = i + 1
Next testRange
End Sub
这应该有效。在您的 set rng 语句中使用 worksheets 参考。这可以清理干净,但我认为您可以通过这种方式更好地理解解决方案。
对您的来源的引用 sheet 已经在您的 rng 变量中。因此,无需在您的 for each 循环条件中再次引用。
这里的语法错误
testValue = Sheets("Issue Reference").testRange.Value
因为 Worksheet Object Sheets("Issue Reference") 没有 testRange 属性.
这是您的代码的更正版本:
Private Sub SubGroupList_Click()
Dim selection As String
Dim rng As Range
Dim testRange As Range
Dim i As Integer
Dim testValue As String
selection = SubGroupList.Value
If failedComponentTrack = 1 Then
Set rng = Sheets("Issue Reference").Range("C3:C10")
ElseIf failedComponentTrack = 2 Then
Set rng = Sheets("Issue Reference").Range("E3:E10")
ElseIf failedComponentTrack = 3 Then
Set rng = Sheets("Issue Reference").Range("G3:G10")
Else
End If
i = 1
For Each testRange In rng
testValue = testRange.Value
If selection = testValue Then
Sheets("DataStore").Cells(LB, 27).Value = selection
failedComponentTrack = i
End If
i = i + 1
Next testRange
End Sub
我正在尝试从 Sheet 中的一系列单元格中提取一个字符串,以在 If 语句中进行测试以确定要执行的操作。调试突出显示以下行作为错误原因:
testValue = Sheets("Issue Reference").testRange.Value
但我不明白为什么这是个问题,也许是 .Value 属性 的问题?有任何想法吗?完整代码如下:
Private Sub SubGroupList_Click()
Dim selection As String
Dim rng As Range
Dim testRange As Range
Dim i As Integer
Dim testValue As String
selection = SubGroupList.Value
If failedComponentTrack = 1 Then
Set rng = Range("C3:C10")
ElseIf failedComponentTrack = 2 Then
Set rng = Range("E3:E10")
ElseIf failedComponentTrack = 3 Then
Set rng = Range("G3:G10")
Else
End If
i = 1
For Each testRange In rng
testValue = Sheets("Issue Reference").testRange.Value
If selection = testValue Then
Sheets("DataStore").Cells(LB, 27).Value = selection
failedComponentTrack = i
End If
i = i + 1
Next testRange
End Sub
这应该有效。在您的 set rng 语句中使用 worksheets 参考。这可以清理干净,但我认为您可以通过这种方式更好地理解解决方案。
对您的来源的引用 sheet 已经在您的 rng 变量中。因此,无需在您的 for each 循环条件中再次引用。
这里的语法错误
testValue = Sheets("Issue Reference").testRange.Value
因为 Worksheet Object Sheets("Issue Reference") 没有 testRange 属性.
这是您的代码的更正版本:
Private Sub SubGroupList_Click()
Dim selection As String
Dim rng As Range
Dim testRange As Range
Dim i As Integer
Dim testValue As String
selection = SubGroupList.Value
If failedComponentTrack = 1 Then
Set rng = Sheets("Issue Reference").Range("C3:C10")
ElseIf failedComponentTrack = 2 Then
Set rng = Sheets("Issue Reference").Range("E3:E10")
ElseIf failedComponentTrack = 3 Then
Set rng = Sheets("Issue Reference").Range("G3:G10")
Else
End If
i = 1
For Each testRange In rng
testValue = testRange.Value
If selection = testValue Then
Sheets("DataStore").Cells(LB, 27).Value = selection
failedComponentTrack = i
End If
i = i + 1
Next testRange
End Sub