vba - For循环工作但指责错误 - VBA
问题描述
我对下面的代码有一点问题。在粗线中,它指责“运行时错误'91':对象变量或未设置块变量”。有趣的是,一切正常,但我不希望它每次都显示此消息。有人知道如何解决吗?非常感谢
Private Sub applybutton_Click()
variablesheet = "Cl" & box_id.Value
maxrow = Sheets("Cl1").Range("A7").Value
Dim i As Long
For i = 1 To maxrow
If Sheets(variablesheet).Cells(i + 19, 1).Value = 0 Then
flavorname = Sheets(variablesheet).Cells(i + 19, 2).Value
**flavor_id = Sheets("products").Range("A:A").Find(flavorname).Offset(0,1).Value**
Sheets(variablesheet).Cells(i + 19, 4).Value = Me.Controls(flavor_id).Value
End If
Next i
解决方案
要控制 find nothing 错误,您的代码应该是这样的(并且以更具体的方式,查看值并查看整个搜索文本):
Private Sub applybutton_Click()
variablesheet = "Cl" & "1" 'box_id.Value
maxrow = Sheets("Cl1").Range("A7").Value
Dim i As Long
For i = 1 To maxrow
If Sheets(variablesheet).Cells(i + 19, 1).Value = 0 Then
flavorname = Sheets(variablesheet).Cells(i + 19, 2).Value
With Sheets("products").Range("A:A")
Set c = .Find(flavorname, LookIn:=xlValues, LookAt:=xlWhole)
If Not c Is Nothing Then
flavor_id = c.Offset(0, 1).Value
Sheets(variablesheet).Cells(i + 19, 4).Value = Me.Controls(flavor_id).Value
End If
End With
End If
Next i
推荐阅读
- analytics - 当没有 Y 值时如何预测测试数据
- javascript - 谷歌登录授权建议
- react-native - 应用程序崩溃:未找到 E/AndroidRuntime + android.support.v4.content.FileProvider 类
- android - 如何将消息从最后一个位置添加到第一个位置?
- c# - 如何通过触摸左右移动?
- python - 如何删除列表中的列表?
- php - php如何从二进制数据中显示文件
- javascript - 通过 concat 显示名称而不显示值来获取变量
- jelastic - jelastic 如何控制新安装的程序并使它们持久化?
- node.js - Error with "solc.loadRemoteVersion" in js