Advice about use of .value in VBA

SR1

Board Regular
Joined
Dec 4, 2007
Messages
77
Hi,

I am working on simplifying my code to make it easier to follow and check for errors, and I am wondering how often the suffix .Value is needed to determine the value of a cell reference.

I would imagine you need the .Value to assign variables (eg x = Cells (r, c).Value) but is it necessary for functions (eg Instr(Cells(x, y).Value, "String") - wouldn't just Cells(x,y) suffice)?

I'm pretty sure you'd need it for other purposes, eg textbox inputs, but is it needed in If statements (like If Cells (x, y).Value = "")?

Any advice on this would be much appreciated.
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
it seems you already have a good understanding of when .value is needed

and basically you can omit the .value as you demonstrate

it comes down to a personal choice as to whether one uses the .value (and same for .value2) when it is not required

personally, I always use the .value
it reminds me everytime that I'm using the value of the range & avoids me accidentally ever trying to assign a range when I should be using the value
for example, variableX = cells(x,y)
when I really want SET rngVariableX = cells(x,y)

often people explain that the quicker omission assuming the default property could be an issue in the future if the default property changes. not a big risk, I think
still I'd rather be explicit

for example
dim rInputTable as Excel.Range
dim vArray as variant

set rInput = range...
vArray = rInput.value2

up to you really, hth
 
Upvote 0

Forum statistics

Threads
1,215,436
Messages
6,124,869
Members
449,192
Latest member
MoonDancer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top