Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Value from a cell on a worksheet

Tags:

excel

vba

I'm trying to do what I think is something simple and looks right. What I'm trying to do is pull a value from a cell on a worksheet that is in the current workbook. However, every time I run the code I get the following error: Run-time error'g': Subscript out of range. Below is the listed code that I'm using.

Damp_DL_Height = ThisWorkbook.Sheets("DampType").Cells(3, 3).Value

I've got the variable defined as Double. DampType is equal to the name of the sheet that I'm trying to pull the cell data from. Should I be using some other type of command to get the value?

Any help is appreciated.

like image 851
Bob Amick Avatar asked Sep 04 '15 18:09

Bob Amick


People also ask

How do you reference a cell across a worksheet?

To reference a cell or range of cells in another worksheet in the same workbook, put the worksheet name followed by an exclamation mark (!) before the cell address. For example, to refer to cell A1 in Sheet2, you type Sheet2!


1 Answers

Since DampType is a string variable, it doesn't need quotation marks -

Damp_DL_Height = ThisWorkbook.Sheets(DampType).Cells(3, 3).Value
like image 132
Raystafarian Avatar answered Sep 27 '22 22:09

Raystafarian