0

I have numeric values on specific cells, the cell's row and column are known. I want to assign the numeric values in these cells to an integer variable in my code that I could then modify and use to update the same cell.

I have tried using the following codes but they did not work because when I add my variables to watch, the value of my variables remained as 0 even the cells have non zero numbers.

    Dim GetTableCount as Integer

    GetTableCount = Cells(17, 13).value        
    GetTableCount = CInt(Cells(17, 13).value)  
    GetTableCount = Range(M17)
    GetTableCount = Range(M17).value 
    GetTableCount = CInt(Range(M17).value)
    GetTableCount = CInt(Range(M17))

I have also tried using the index function but I failed on looking up the right syntax. My last attempt to solve the problem is to specify the cells to "Numbers" format instead of "General" but still nothing.

Community
  • 1
  • 1
  • 1
    It's `Range("M17")` unless you have a variable named `M17` which hold a range address. `GetTableCount = CInt(Cells(17, 13).Value)` should work. If you're debugging, you have to step *past* the assignment before `GetTableCount` will indicate a value. – Tim Williams Aug 30 '16 at 00:29
  • `GetTableCount = Val([M17])` – Slai Aug 30 '16 at 00:32
  • There is no M17 variable in my code. I have tried overstepping through this line GetTableCount = CInt(Cells(17, 13).Value) But the value on debug mode is still 0, and not the value of the cell that I specified. I will double check this again tomorrow. – nakakapagpabagabag Aug 30 '16 at 03:19
  • None of these will work if you are interrogating a worksheet that is not the active one. Try putting a `With ... End With` block around your code, eg `With Worksheets("Sheet1")|GetTableCount = .Cells(17,13).Value` – Ambie Aug 30 '16 at 03:45
  • `@Ambie`, you are right , I did have these codes prior to assigning the cell value to my variables: `Sheets("ADMIN").Visible = True` `Sheets("ADMIN").Select` where the ADMIN sheet is where I place my integer values in a cell, `@Tim @Slai` but still none of the solutions work. – nakakapagpabagabag Aug 30 '16 at 16:17
  • It turns out that `anyintvar = Cells(row, column)` works but the code is reading it from a different worksheet so the value remains at 0, with the cell from wherever sheet the code is reading seems to be empty. Even I had these lines for sheet selections `Sheets("ADMIN").Visible = True` and `Sheets("ADMIN").Select` the code still does not read from the selected sheet. Using this: `GetTableCount = ActiveWorkbook.Worksheets("ADMIN").Cells(17, 13)` fixed the problem. I did not even have to write these lines anymore `Sheets("ADMIN").Visible = True` `Sheets("ADMIN").Select` kudos to @jpinto3912 – nakakapagpabagabag Aug 30 '16 at 17:39
  • Thank you for helping out Tim, Slai and Ambie. – nakakapagpabagabag Aug 30 '16 at 17:43

1 Answers1

0

It turns out that

anyintvar = Cells(row, column)

works but the code is reading it from a different worksheet so the value remains at 0, with the cell from wherever sheet the code is reading seems to be empty.

Even though I had these lines for sheet selections

Sheets("ADMIN").Visible = True

Sheets("ADMIN").Select

the code still does not read from the selected sheet.

Using this however:

GetTableCount = ActiveWorkbook.Worksheets("ADMIN").Cells(rownumber, columnnumber)

fixed the problem. I did not even have to write the pervious two lines anymore. Kudos to @jpinto3912 Page where I found the solution

Community
  • 1
  • 1