Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBA Worksheet Basics

Posted on 2011-03-02
7
Medium Priority
?
223 Views
Last Modified: 2012-05-11

I'm trying to get the basics of the objects, properties, and methods down, but can't find any great online resources.  In the meantime, I guess actual examples is the best way to go.  I have some code that I got from one of the experts here in a previous question (http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26856102.html).  The problem was, in the example I provided, the data was on the same page, when I'm actually linking to and importing from an Access database onto a separate Sheet I call "QB_Expenses".  I'm trying to walk through the code so I can learn and adjust.  My first sticking point is:
 lastRow = Range("A" & Rows.Count).End(xlUp).Row

how do I change that so that I'm finding the last row on the QB_Expenses sheet instead of the current sheet?
0
Comment
Question by:BBlu
  • 4
  • 3
7 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 1200 total points
ID: 35022600
you can try this:

 lastRow = Sheets("QB_Expenses").Range("A" & Rows.Count).End(xlUp).Row
0
 

Author Comment

by:BBlu
ID: 35022634
got it.  so Sheets is the object, Range is a property (of Sheets). and an object? Are End and Row considered properties, methods?
0
 
LVL 22

Assisted Solution

by:rspahitz
rspahitz earned 1200 total points
ID: 35022697
Pretty much.

the way most things work these days is that objects have properties and possibly additional nested objects, which are treated like properties.

So from the Application object of Excel, you have the WorkBook objects, which have the WorkSheet objects, which have several objects such as the Range,Row and Column objects; the Range object has cells, etc.

Typically, and item that you see with parentheses after it is either an array (like Sheets) or a method (and action that is either a subroutine or a function) like the End method.  Any method can have zero or more parameter values such as xlUp for the End method.

If there are no parentheses then it's probably a property (although VB is careless about this so sometimes it's still a method without parameters.)
when working with properties, then you can usually assign it a value with = xxx (although some properties are read-only)

the easiest way to tell the difference is to let Vb help.  Type the first few letters of on of these things and press Ctrl+J; you should get an intellisense drop-down window.  If you see a little hand holding a paper, it's a property or embedded object (blue box in VB.Net); green box is a method (purple in VB.Net); yellow lightning bolt for an event.  you'll also see other symbols for things like libraries constants and enumerations, etc.

Hope that helps a little.

0
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.

 

Author Comment

by:BBlu
ID: 35022726
Thanks, rspahitz.  That is the perfect explanation!
0
 

Author Comment

by:BBlu
ID: 35022735
Before I close out this question, is there a way to keep the current sheet selected rather than switch when I perform the code:

 lastRow = Sheets("QB_Expenses").Range("A" & Rows.Count).End(xlUp).Row
0
 
LVL 22

Assisted Solution

by:rspahitz
rspahitz earned 1200 total points
ID: 35022767
I think that works behind the scenese (although some of Excel's method require that the sheet be active.

The best way to ensure that the current sheet is active when done is to save it, perform the action, then restore it.  In this case, you use the "object" syntax ("Set") of VB:

    Dim objSaveSheet As Worksheet
    Set objSaveSheet = ActiveSheet

' do what you need to
'e.g.    Sheets("Sheet2").Activate

    objSaveSheet.Activate
    Set objSaveSheet = Nothing
0
 

Author Closing Comment

by:BBlu
ID: 35023321
Great help, as always.  Thanks, rspahitz!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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