Selasa, 13 Desember 2011

[ExcelVBA] Help! There is an evil genie in my VBA

 

To illustrate my puzzle, I've set up a workbook with 2 sheets, named Vegs and Fruit. the Vegs sheet has Beets Corn Spinach in A1, A2 and A3. The Fruit sheet has Apple Banana Cherry in B1, B2 and B3.

I wrote the following code:

Sub Test()
Dim FruitName As String
Dim VegName As String

1 Sheets("Vegs").Select
2 VegName = Range("A2")
3 Sheets("Fruit").Select
4 FruitName = Range("B1")
5 VegName = Sheets("Vegs").Range("A2")
End Sub

I initially displayed the Fruit sheet, and when stm 1 was executed, the Vegs sheet was displayed. But after stm 2 was executed, VegName was empty, i.e., "". That is the first puzzle - VegName should be Corn.
When stm 3 was executed the Fruit sheet was displayed. When stm 4 was executed FruitName was Apple. That indicates to me that stms 1 and 2, which are similar to stms 3 and 4, should work. To put icing on the puzzle, after stm 5, which is equivalent, I think, to stms 1 and 2, is executed, VegName finally had the value Corn.
I also noticed when I hover the mouse over Range("A2") in stms 2 or 5 nothing displays, but when I hover the mouse over Range("B1") in stm 4, a tooltip displays Range("B1")="Apple".

What in the world is going on?
Thanks.

__._,_.___
Recent Activity:
----------------------------------
Be sure to check out TechTrax Ezine for many, free Excel VBA articles! Go here: http://www.mousetrax.com/techtrax to enter the ezine, then search the ARCHIVES for EXCEL VBA.

----------------------------------
Visit our ExcelVBA group home page for more info and support files:
http://groups.yahoo.com/group/ExcelVBA

----------------------------------
More free tutorials and resources available at:
http://www.mousetrax.com

----------------------------------
MARKETPLACE

Stay on top of your group activity without leaving the page you're on - Get the Yahoo! Toolbar now.

.

__,_._,___

Tidak ada komentar:

Posting Komentar