Guizmoo03
Guizmoo03

Reputation: 660

Cells.value doesn't display any value in a msgBox in VBA

I'm new with VBA and I'm trying to display the value of cells in msgBox (one after another) when looping through a given column, but the message boxes does not show the values (these ones are not empty or "" in the worksheet).

My code is the following:

For i=2 To workbooks(filename).Worksheets(1).Columns(columnNumber).rows.count
    MsgBox "Cell value is: " & Cells(i, columnNumber).address 
    MsgBox "Cell value is: " & Cells(i, columnNumber).value
Next i

The problem is that the addresses are correctly displayed in the MsgBoxes while the values are not. any help will be much appreciated.

Upvotes: 1

Views: 2386

Answers (1)

Guizmoo03
Guizmoo03

Reputation: 660

It was actually the reference to the correct workbook that was missing. Thanks Rory for your help.

The correct code is as follow:

For i=2 To Workbooks(filename).Worksheets(1).Columns(columnNumber).Rows.Count
   MsgBox "Cell value is: " & Workbooks(filename).Worksheets(1).Cells(i, columnNumber).Address 
   MsgBox "Cell value is: " & Workbooks(filename).Worksheets(1).Cells(i, columnNumber).Value
Next i

Now it works perfectly.

Upvotes: 1

Related Questions