Solved

Why do i need to add the property .value

Posted on 2014-03-07
6
180 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 80

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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 80

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now