vba Excel VBA中的对象需要错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19048601/
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
Object Required Error in excel VBA
提问by srt
Dim g1val, g2val As Integer
Set g1val = 0
Set g2val = 0
For i = 3 To 18
If g1val > Cells(33, i).Value Then
g1val = g1val
Else
g1val = Cells(33, i).Value
End If
Next i
For j = 32 To 57
If g2val > Cells(31, j).Value Then
g2val = g2val
Else
g2val = Cells(31, j).Value
End If
Next j
Here on second line i am getting an error saying object required.I have tried to make g1val and g2val as "Double" and tried to give 1 for their values at first.But those didn't work out. Can u help??....
在第二行,我收到一个错误,提示需要对象。我尝试将 g1val 和 g2val 设为“Double”,并一开始尝试将它们的值设为 1。但这些都没有成功。你能帮忙吗??...
采纳答案by Santosh
In order to set the value of integer variable we simply assign the value to it.
eg g1val = 0
where as set keyword is used to assign value to object.
为了设置整数变量的值,我们只需将值分配给它。例如g1val = 0
,其中 as set 关键字用于为对象赋值。
Sub test()
Dim g1val, g2val As Integer
g1val = 0
g2val = 0
For i = 3 To 18
If g1val > Cells(33, i).Value Then
g1val = g1val
Else
g1val = Cells(33, i).Value
End If
Next i
For j = 32 To 57
If g2val > Cells(31, j).Value Then
g2val = g2val
Else
g2val = Cells(31, j).Value
End If
Next j
End Sub
回答by Marcus Mangelsdorf
The Set statementis only used for object variables (like Range
, Cell
or Worksheet
in Excel), while the simple equal sign'=' is used for elementary datatypes like Integer
. You can find a good explanation for when to use set here.
该Set语句仅用于对象变量(如Range
,Cell
或Worksheet
在Excel中),而简单的等号“=”用于类的基本数据类型Integer
。您可以在此处找到有关何时使用 set的很好解释。
The other problem is, that your variable g1val
isn't actually declared as Integer
, but has the type Variant
. This is because the Dim statementdoesn't work the way you would expect it, here (see example below). The variable has to be followed by its type right away, otherwise its type will default to Variant
. You can only shorten your Dim statement this way:
另一个问题是,您的变量g1val
实际上并未声明为Integer
,而是具有类型Variant
。这是因为Dim 语句在此处无法按您期望的方式工作(请参见下面的示例)。变量必须紧跟其类型,否则其类型将默认为Variant
。您只能通过这种方式缩短 Dim 语句:
Dim intColumn As Integer, intRow As Integer 'This creates two integers
For this reason, you will see the "Empty" instead of the expected "0" in the Watches window.
因此,您将在 Watches 窗口中看到“Empty”而不是预期的“0”。
Try this example to understand the difference:
试试这个例子来理解不同之处:
Sub Dimming()
Dim thisBecomesVariant, thisIsAnInteger As Integer
Dim integerOne As Integer, integerTwo As Integer
MsgBox TypeName(thisBecomesVariant) 'Will display "Empty"
MsgBox TypeName(thisIsAnInteger ) 'Will display "Integer"
MsgBox TypeName(integerOne ) 'Will display "Integer"
MsgBox TypeName(integerTwo ) 'Will display "Integer"
'By assigning an Integer value to a Variant it becomes Integer, too
thisBecomesVariant = 0
MsgBox TypeName(thisBecomesVariant) 'Will display "Integer"
End Sub
Two further notices on your code:
关于您的代码的另外两个通知:
First remark:Instead of writing
第一句话:代替写作
'If g1val is bigger than the value in the current cell
If g1val > Cells(33, i).Value Then
g1val = g1val 'Don't change g1val
Else
g1val = Cells(33, i).Value 'Otherwise set g1val to the cell's value
End If
you could simply write
你可以简单地写
'If g1val is smaller or equal than the value in the current cell
If g1val <= Cells(33, i).Value Then
g1val = Cells(33, i).Value 'Set g1val to the cell's value
End If
Since you don't want to change g1val
in the other case.
因为你不想g1val
在另一种情况下改变。
Second remark:I encourage you to use Option Explicitwhen programming, to prevent typos in your program. You will then have to declare all variables and the compiler will give you a warning if a variable is unknown.
第二点:我鼓励你在编程时使用Option Explicit,以防止你的程序出现拼写错误。然后,您必须声明所有变量,如果变量未知,编译器会向您发出警告。