Joseph Park
Joseph Park

Reputation: 3

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

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

Upvotes: 0

Views: 97

Answers (1)

BruceWayne
BruceWayne

Reputation: 23285

Guessing, since it's a simple macro, but the quotes you used (assuming what we see here is exactly what's in your Module/Sheet code) may not work with VBA.

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

As far as @KenWhite asking where your code is, it's either in a Worksheet, or Module. I put the above in a Worksheet which you can see in the VBEditor window.

enter image description here

Upvotes: 3

Related Questions