Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why do i need to add the property .value

Posted on 2014-03-07
6
Medium Priority
?
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

670 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