user2758599
user2758599

Reputation: 1

Simple vba program in Excel

Sub TEST()
    If cells(i, "R").Value <> "UK" Then
        cells(i, "R").Interior.ColorIndex = 3
    End If
End Sub

If I run this program it throws application defined error \

I am new to Excel (beginner)

How to correct this error!!!

Thanks In advance

Upvotes: 0

Views: 119

Answers (3)

Gary&#39;s Student
Gary&#39;s Student

Reputation: 96753

The problem is that the variable i has not been assigned a value. VBA assumes that it is zero. Since i is used to determine the row of the cell, Excel throws an exception because there is no row 0!

Upvotes: 2

Mahdi Jazini
Mahdi Jazini

Reputation: 791

First you have to define i variable

for example: Dim i as variant

Upvotes: 1

Bob
Bob

Reputation: 303

I think the issue is "R" that I know of the cells method takes 2 parameters one is rows the other is columns (in that order) but this is done by number not letter so if you change it to cell(1,18) then the code above works fine.

This link may also be useful to learn more, among other things it describes how you would normally select a range first as I believe your code above will assume the currently selected page, however you might want to run in on a button click from another page or as soon as the spreadsheet opens.

http://msdn.microsoft.com/en-us/library/office/ff196273.aspx

Upvotes: 2

Related Questions