应用程序定义或对象定义的错误,血腥的奥秘
我是一个相对无知的程序员,很抱歉。应用程序定义或对象定义的错误,血腥的奥秘
Excel 2010 UserForm命令按钮的下列代码返回应用程序定义或对象定义的错误。
Private Sub CommandButton1_Click()
Dim y, vfx, sapx, prx As String
y = Sheets(ComboBox1.Value).Range("B:B").Find(ComboBox2.Value).Row
vfx = Sheets(ComboBox1.Value).Range("1:1").Find("Vendor No").Column
sapx = Sheets(ComboBox1.Value).Range("1:1").Find("SAP Number").Column
prx = Sheets(ComboBox1.Value).Range("1:1").Find("Amey Price").Column
Lrow = Sheets("Macropage").Range("B:B")(Rows.Count).End(xlUp).Row
Userow = Lrow + 1
Sheets("Macropage").Range("A" & Userow) = ComboBox1.Value
Sheets("Macropage").Range("B" & Userow) = ComboBox2.Value
Sheets("Macropage").Range("C" & Userow) = Sheets(ComboBox1.Value).Cells(y, vfx).Value
Sheets("Macropage").Range("D" & Userow) = Sheets(ComboBox1.Value).Cells(y, sapx).Value
Sheets("Macropage").Range("E" & Userow) = Sheets(ComboBox1.Value).Cells(y, prx).Value
Sheets("Macropage").Range("A3:E" & Userow).Columns.AutoFit
Unload UserForm1
End Sub
导致错误的代码看起来是:Sheets(ComboBox1.Value).Cells(y, prx).Value
我通过在一个MsgBox粘它验证。
prx
引用的列唯一不同的地方是它包含小数而不是一些文本。我不明白为什么这应该是一个问题,但。
任何感兴趣的信息,即使它只是关于应用程序定义或对象定义的错误的性质。从谷歌搜索它似乎只是意味着即使编译器认为它是好的,Excel执行脚本时仍有问题。
天啊,我是个白痴。马特的马克杯指出我,prx
是一个字符串 - 所以Cells()
不能正确解释它作为列索引。
不完全确定为什么我认为var1, var2, var3 as type
会对所有人应用相同的类型。我以为我看过变量声明之前..
谢谢大家。回答我自己的问题是可以的,对吧?
你可能想看看[Rubberduck](http://www.rubberduck-vba.com),我的小宠物项目。最新版本(v1.4.3)中的解析器将炸开包装指令,例如Sheets(ComboBox1.Value).Range(“B:B”)。Find(ComboBox2.Value).Row' and'Macropage.Range (“B:B”)(Rows.Count).End(xlUp).Row',但是如果你可以简化这些调用,提取局部变量并获得Rubberduck解析器来理解你的代码(v2.0将有更好的解析器),我认为*代码检查*功能将特别有用。它会拿起这个“带有多个声明的指令”。 –
这不是问题的解决方案,而是重写代码以更好地跟踪错误。
Private Sub CommandButton1_Click()
On Error GoTo ErrExit
Dim y, vfx, sapx, prx As String
Dim sheet, Macropage
Set thisSheet = Sheets(ComboBox1.Value)
Set Macropage = Sheets("Macropage")
If thisSheet Then
MsgBox "You have to select an input page", vbExclamation
Exit Sub
End If
If Macropage Then
MsgBox "You have to select a Macro page", vbExclamation
Exit Sub
End If
y = thisSheet.Range("B:B").Find(ComboBox2.Value).Row
vfx = thisSheet.Range("1:1").Find("Vendor No").Column
sapx = thisSheet.Range("1:1").Find("SAP Number").Column
prx = thisSheet.Range("1:1").Find("Amey Price").Column
Lrow = Macropage.Range("B:B")(Rows.Count).End(xlUp).Row
Userow = Lrow + 1
Macropage.Range("A" & Userow) = ComboBox1.Value
Macropage.Range("B" & Userow) = ComboBox2.Value
Debug.Print "y= " & y, "vfx= " & vfx, "sapx= " & sapx, "prx= " & prx
Macropage.Range("C" & Userow) = thisSheet.Cells(y, vfx).Value
Macropage.Range("D" & Userow) = thisSheet.Cells(y, sapx).Value
Macropage.Range("E" & Userow) = thisSheet.Cells(y, prx).Value
Macropage.Range("A3:E" & Userow).Columns.AutoFit
SafeExit:
Unload UserForm1
Exit Sub
ErrExit:
MsgBox Error, vbCritical, "Error"
Resume SafeExit
End Sub
请发布Debug.Print的字符串以获取有关错误的想法 – milevyo
您好,我通过从我声明的变量中除去'as string'来消除错误。 'cells()'不能正确解释'prx'作为字符串。 (请参阅我的初始帖子中的马克杯评论)。 感谢您的回应,但如果我从不需要寻找错误原因,我很可能会提及这一点。 – CapriciousMelon
你确定'prx'有一个值吗?我会在该线上放置一个断点,并查看它在本地窗口中的价值。 –
而且,如果它没有价值,我的猜测是没有列“Amey价格”。 –
'Dim y,vfx,sapx,prx As String' - 与您的问题无关,但您意识到只有'prx'实际上被声明为'String'的权利?其他人被隐式声明为“变体”。 –