Solved

VBA Worksheet Basics

Posted on 2011-03-02
7
220 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
[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
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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 tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

707 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