VBA: Setting A Variable To A Specific Cell Value [closed] - Stack Overflow

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

Closed 2 years ago.

Improve this question

In Sheet1 of my workbook, cell B2 has the value 29.

I want to assign a cell value to a variable, num_g.

I've declared num_g as Integer and am trying to assign its value with the code:

num_g = Worksheets("Sheet1!").Cells(1, 2).Value

I'm using Cells() as I plan to replace the row with a counter.

However, I keep getting the "Subscript out of range" error (run-time error 9), and don't understand why.

How can I fix this? Declaring num_g as Long or String doesn't help, nor does formatting cell B2 as a number.

Thanks in advance.

Từ khóa » Visual Basic Excel Set Cell Value