Reference a cell using a variable

I'm trying to use a variable value in a macro to reference a cell.   For example, I currently some code that looks like this:

If Cells( 5,10).value = "" then
   msgbox "cell J5 is empty"

Let's say I have an Integer variable named MyVariable and I assign a value of 5 to it.

I'm trying to code something like:

If Cells(MyVariable,10).Value = "" then
   msgbox "cell J5 is empty"

but I can't get it to work.   I don't care about using the "Cells" reference method. I'm just trying to use any method where a cell can be referenced using a variable like the one I've drescribed above.

Thank you
dbfromnewjerseyAsked:
Who is Participating?
 
Keyboard CowboyConnect With a Mentor Commented:
for one thing, you need and end if (perhaps that got cut off).  You don't really need .value
This worked for me
Sub test()
Dim n As Integer
n = 5

If Cells(n, 10).Value = "" Then
    MsgBox "empty"
    End If
   
End Sub
0
 
NorieVBA ExpertCommented:
That code looks fine to me, Cells(MyVariable, 10) should refer to J5 on the active sheet.

What problems are you having?
0
 
Keyboard CowboyCommented:
Of course - this also works on a single line
If Cells(n, 10).Value = "" Then MsgBox "empty"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.