0

It would be very basic question.

I want to declare cells as variant, to substitute repeated typing, like

a = cells(1,1) or a = range("a1")

then a.select or a.value, etc..

but error occur with

runtime 424

I want to understand what is problem, and how can i do.

Thanks,

3
  • 3
    You have to declare a range and then set it. Like Dim a as Range and Set a = .Range("A1"). Runtime error 424 tells you there is no range object declared. Keyword here is Set which is used to actually create a range object. Commented Jul 29, 2019 at 11:33
  • You are assigning the value of a cell to a variable, which does not have a value property and cannot be selected. Commented Jul 29, 2019 at 11:38
  • Also using .Cells or .Range without explicitly declaring in what workbook and worksheet they are is prone to errors. You should take a look at stackoverflow.com/questions/10714251/… Commented Jul 29, 2019 at 11:39

1 Answer 1

2

You've run into an interesting 'feature' of VBA - the Default Property. Most objects in the Excel Library have a default property, which is used where no property is specified. In the case of the range object the default property is the Value property This means that the line

 Range("A1") = 4 

is interpreted by VBA as meaning

 Let Range("A1").Value = 4 

In your case, the line

 a = cells(1,1) 

is ultimately interpreted as

 Let a = range("a1").value 

However, had you written

 Set a = cells(1,1) 

then that set forces vba to treat cells(1,1) as an object and thus to set a to act as a pointer to the cell A1, which is what you wanted.

(Had VBA still insisted on using Let for value assignments no confusion would be possible as your original line would have errored for ambiguity - but the Let statement is now optional so you don't get an error if you leave it off)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.