Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


VBA Worksheet Basics

Posted on 2011-03-02
Medium Priority
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?
Question by:BBlu
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
  • 4
  • 3
LVL 22

Accepted Solution

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

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

Author Comment

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?
LVL 22

Assisted Solution

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.

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

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

Author Comment

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
LVL 22

Assisted Solution

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

    Set objSaveSheet = Nothing

Author Closing Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

650 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