Solved

VBA Worksheet Basics

Posted on 2011-03-02
7
208 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 300 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 300 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 300 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now