需要 Excel VBA 运行时错误“424”对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21358540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:37:27  来源:igfitidea点击:

Excel VBA Run Time Error '424' object required

excelvbaexcel-vba

提问by user3232996

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.

我在 VBA 和一般编码方面是全新的,我试图从同一个工作簿(获取框架路径...)的单元格中获取数据,然后启动应用程序(QTP)并运行测试。

I am getting this error when trying to get values entered in excel cells:

尝试获取在 excel 单元格中输入的值时出现此错误:

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

回答by Portland Runner

The first code line, Option Explicitmeans (in simple terms) that all of your variables have to be explicitly declaredby Dimstatements. They can be any type, including object, integer, string, or even a variant.

第一行代码Option Explicit意味着(简单来说)所有变量都必须通过语句显式Dim声明。它们可以是任何类型,包括对象、整数、字符串,甚至是变体。

This line: Dim envFrmwrkPath As Rangeis declaring the variable envFrmwrkPathof type Range. This means that you can only set it to a range.

这一行:Dim envFrmwrkPath As Range声明了envFrmwrkPathtype的变量Range。这意味着您只能将其设置为一个范围。

This line: Set envFrmwrkPath = ActiveSheet.Range("D6").Valueis attempting to set the Rangetype 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.

这一行:Set envFrmwrkPath = ActiveSheet.Range("D6").Value试图将Range类型变量设置为单元格中的特定值D6。例如,这可能是一个整数或一个字符串(取决于您在该单元格中的内容),但它不是一个范围。

I'm assuming you want the value stored in a variable. Try something like this:

我假设您希望将值存储在变量中。尝试这样的事情:

Dim MyVariableName As Integer
MyVariableName = ActiveSheet.Range("D6").Value

This assumes you have a number (like 5) in cell D6. Now your variable will have the value.

这假设您在单元格 D6 中有一个数字(如 5)。现在您的变量将具有该值。

For simplicity sake of learning, you can remove or comment out the Option Explicitline and VBA will try to determine the type of variables at run time.

为简单起见,您可以删除或注释掉该Option Explicit行,VBA 将在运行时尝试确定变量的类型。



Try this to get through this part of your code

试试这个来完成这部分代码

Dim envFrmwrkPath As String
Dim ApplicationName As String
Dim TestIterationName As String

回答by Sandeep Bhatt

Simply remove the .valuefrom your code.

只需.value从您的代码中删除。

Set envFrmwrkPath = ActiveSheet.Range("D6").Value

instead of this, use:

而不是这个,使用:

Set envFrmwrkPath = ActiveSheet.Range("D6")

回答by JPR

You have two options,

你有两个选择,

-If you want the value:

- 如果你想要这个值:

Dim MyValue as Variant ' or string/date/long/...
MyValue = ThisWorkbook.Sheets(1).Range("A1").Value

-if you want the cell object:

- 如果你想要单元格对象:

Dim oCell as Range  ' or object (but then you'll miss out on intellisense), and both can also contain more than one cell.
Set oCell = ThisWorkbook.Sheets(1).Range("A1")

回答by user3736273

Private Sub CommandButton1_Click()

    Workbooks("Textfile_Receiving").Sheets("menu").Range("g1").Value = PROV.Text
    Workbooks("Textfile_Receiving").Sheets("menu").Range("g2").Value = MUN.Text
    Workbooks("Textfile_Receiving").Sheets("menu").Range("g3").Value = CAT.Text
    Workbooks("Textfile_Receiving").Sheets("menu").Range("g4").Value = Label5.Caption

    Me.Hide

    Run "filename"

End Sub

Private Sub MUN_Change()
    Dim r As Integer
    r = 2

    While Range("m" & CStr(r)).Value <> ""
        If Range("m" & CStr(r)).Value = MUN.Text Then
        Label5.Caption = Range("n" & CStr(r)).Value
        End If
        r = r + 1
    Wend

End Sub

Private Sub PROV_Change()
    If PROV.Text = "LAGUNA" Then
        MUN.Text = ""
        MUN.RowSource = "Menu!M26:M56"
    ElseIf PROV.Text = "CAVITE" Then
        MUN.Text = ""
        MUN.RowSource = "Menu!M2:M25"
    ElseIf PROV.Text = "QUEZON" Then
        MUN.Text = ""
        MUN.RowSource = "Menu!M57:M97"
    End If
End Sub