Solved

Why do i need to add the property .value

Posted on 2014-03-07
6
182 Views
Last Modified: 2014-03-08
If I have strTest = Range("a100") then "" is assigned to strTest
strTest = Range("a100") .Value then strTest is assigned a value

Can someone educate me on when I would not use .Value ?  Is .Value only needed if I doing a test compare ?
0
Comment
Question by:upobDaPlaya
  • 3
  • 2
6 Comments
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 250 total points
ID: 39914119
Every object has a default property. In this case the Range object's default property is Value. However, the default value is not ALWAYS used. In some cases the Range object itself will be assigned to the variable.

In general it is good practice to always include the property even if it is the default property. This avoids ambiguity and makes it clear to others looking at your code what you are doing. It's also good practice to qualify everything. The Range object parent is implied in your case. If I were writing the code I would do it like this:

Dim strTest as String
strTest = ThisWorkbook.Sheets("Sheet 1").Range("a100").Value

This way we know exactly what is going on. No ambiguity, including the type of the variable strTest.

Kevin
0
 

Author Comment

by:upobDaPlaya
ID: 39914135
zorvek..that makes sense, but leads to another question..above my line I provided in the initial post I also have
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
With ThisWorkbook.Sheets("Sheet1")
strTest = Range("a100") .Value
........................................
...........................................
End With

When do I need to set and do I need to explicity name my wb and ws in the with line if I use your line of code ?
0
 
LVL 81

Accepted Solution

by:
byundt earned 250 total points
ID: 39914230
upobDaPlaya,
In the example you cited, the variable ws is fully qualified, so you could write your code like this:
Set wb = ThisWorkbook 
Set ws = wb.Worksheets("Sheet1")
With ws
      strTest = .Range("A100") .Value 

Open in new window

In the last statement, note the dot in front of Range. That dot makes an implicit reference to ws. Without that dot, Range("A100").Value refers to the value of cell A100 on whichever worksheet is active--and that may not necessarily be Sheet1.

You can also set your With block up all at once. You might do this if you don't need to refer to either wb or ws in other parts of your code.
With ThisWorkbook.Worksheets("Sheet1")
     strTest = .Range("A100").Value

Open in new window

An unqualified reference might still be interpreted correctly if ThisWorkbook happens to be active and Sheet1 is the active worksheet. I agree with Kevin that this is not a good practice. Better to fully qualify than to count on always being lucky.
strTest = Range("A100").Value       'Since Range isn't qualified, it applies to the active worksheet

Open in new window

Brad
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:upobDaPlaya
ID: 39915084
If I have another sub that use the variable wb and ws I assume I need to declare wb and ws in this other sub
0
 
LVL 81

Expert Comment

by:byundt
ID: 39915110
You can pass wb or ws to the other sub as variables. Otherwise, you need to declare them and set them in that other sub.

Sub Sub1()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Sheet1")
Call Sub2 ws       'Upon return, ws will be named Sheet13
End Sub

Sub Sub2(wksht As Worksheet)
wksht.Name = "Sheet13"
End Sub

Open in new window

0
 

Author Closing Comment

by:upobDaPlaya
ID: 39915385
Very well explained and fantastic examples.  The "Best Practices" suggestions our great for my Excel foundation...many thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question