Solved

Why do i need to add the property .value

Posted on 2014-03-07
6
183 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

749 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