How to fix "subscript out of range" error in a simple code

时间:2019-04-17 02:46:31

标签: excel vba

I'm starting to learn how to write VBA codes with the VBA for Dummies book. There is this code that I literally copy and paste from the book but it gives me an error.

Can you please help?

Sub ShowValue()

    Contents = Worksheets(“Sheet1”).Range(“A1”).Value
    MsgBox Contents

End Sub

1 个答案:

答案 0 :(得分:2)

猜测,因为它是一个简单的宏,但是您使用的引号(假设我们在此处看到的正是您的Module / Sheet代码中的内容)可能不适用于VBA。

Sub ShowValue()
Dim contents As String
    contents = Worksheets("Sheet1").Range("A1").Value
    MsgBox (contents)
End Sub

@KenWhite询问您的代码在哪里,它们在工作表或模块中。我将以上内容放在工作表中,您可以在VBEditor窗口中看到它。

enter image description here