Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Why do i need to add the property .value

Posted on 2014-03-07
6
Medium Priority
?
188 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 1000 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

927 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