需要Excel VBA运行时错误'424'对象 [英] Excel VBA Run Time Error '424' object required
问题描述
我在VBA和编码方面完全陌生,我试图从同一工作簿的单元格中获取数据(获取框架路径...),然后启动应用程序(QTP)并运行测试.
I am totally new in VBA and coding in general, am trying to get data from cells from the same workbook (get framework path ...) and then to start application (QTP) and run tests.
尝试获取在excel单元格中输入的值时出现此错误:
I am getting this error when trying to get values entered in excel cells:
Run Time Error '424' object required
我相信我缺少一些基本规则,但感谢您的帮助.请在下面的代码部分中查看:
I believe I am missing some basic rules but I appreciate your help. Please see below the part of code in question:
Option Explicit
Private Sub RunTest_Click()
Dim envFrmwrkPath As Range
Dim ApplicationName As Range
Dim TestIterationName As Range
'Dim wb As Workbook
'Dim Batch1 As Worksheets
Dim objEnvVarXML, objfso, app As Object
Dim i, Msgarea
Set envFrmwrkPath = ActiveSheet.Range("D6").Value ' error displayed here
Set ApplicationName = ActiveSheet.Range("D4").Value
Set TestIterationName = ActiveSheet.Range("D8").Value
推荐答案
第一行代码Option Explicit
(简单地表示)必须由明确声明所有变量c1>语句.它们可以是任何类型,包括对象,整数,字符串甚至是变体.
The first code line, Option Explicit
means (in simple terms) that all of your variables have to be explicitly declared by Dim
statements. They can be any type, including object, integer, string, or even a variant.
此行:Dim envFrmwrkPath As Range
声明类型为Range
的变量envFrmwrkPath
.这意味着您只能将其设置为一个范围.
This line: Dim envFrmwrkPath As Range
is declaring the variable envFrmwrkPath
of type Range
. This means that you can only set it to a range.
此行:Set envFrmwrkPath = ActiveSheet.Range("D6").Value
试图将Range
类型变量设置为单元格D6
中的特定值.例如,它可以是整数或字符串(取决于该单元格中的内容),但这不是范围.
This line: Set envFrmwrkPath = ActiveSheet.Range("D6").Value
is attempting to set the Range
type variable to a specific Value that is in cell D6
. This could be a integer or a string for example (depends on what you have in that cell) but it's not a range.
我假设您希望将值存储在变量中.尝试这样的事情:
I'm assuming you want the value stored in a variable. Try something like this:
Dim MyVariableName As Integer
MyVariableName = ActiveSheet.Range("D6").Value
这假设您在单元格D6中有一个数字(如5).现在,您的变量将具有值.
This assumes you have a number (like 5) in cell D6. Now your variable will have the value.
为简单起见,您可以删除或注释掉Option Explicit
行,VBA会在运行时尝试确定变量的类型.
For simplicity sake of learning, you can remove or comment out the Option Explicit
line and VBA will try to determine the type of variables at run time.
尝试此操作以遍历代码的这一部分
Try this to get through this part of your code
Dim envFrmwrkPath As String
Dim ApplicationName As String
Dim TestIterationName As String
这篇关于需要Excel VBA运行时错误'424'对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!