开发者

Simple variable assignment in Excel 2003 VBA

开发者 https://www.devze.com 2022-12-22 17:22 出处:网络
I am new to VBA in Excel.I\'m setting up a simple macro Option E开发者_如何学编程xplicit Sub Macro1()

I am new to VBA in Excel. I'm setting up a simple macro

Option E开发者_如何学编程xplicit

Sub Macro1()
   Dim sheet
   sheet = Worksheets.Item(1)  ' This line has the error
End Sub

On the line with the error, I get "Run-time error '438' Object doesn't support this property or method"

I can use the Watch window to see that "Worksheets.Item(1)" is a valid object. I've tried changing it to "Dim sheet As Worksheet" but same result.

Ok, so what am I missing? Why does this error occur?

Thanks!

-Mike


You need a Set statement (as you are assigning a reference):

Option Explicit

Sub Macro1()
   Dim sheet As Worksheet
   Set sheet = Worksheets.Item(1) 

   '' ... Use sheet

   Set sheet = Nothing
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号