Database
  Home arrow Database arrow Formatting Techniques for Data Access from...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE

Formatting Techniques for Data Access from Excel VBA
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2006-12-21

    Table of Contents:
  • Formatting Techniques for Data Access from Excel VBA
  • Formatting Techniques Continued
  • Formatting Techniques with Style
  • Formatting Techniques Example
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Formatting Techniques for Data Access from Excel VBA


    (Page 1 of 5 )

    In this article, the second of two parts, you will learn about the two formatting methods you can use when dealing with Excel, Access, and VBA. It is excerpted from chapter three of the book Integrating Excel and Access, written by Michael Schmalz (O'Reilly, 2005; ISBN: 0596009739). Copyright © 2005 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    Formatting Techniques

    There are essentially two formatting methods, which are on a worksheet using VBA. The first method is to learn where all of the formatting options are and write the code from scratch. This method is most effective when doing the same thing many times (formatting numbers as currency, for example); however, trying to remember how to perform every type of formatting becomes a daunting task. So, this brings us to the second method: using the Macro Recorder.

    To access the Macro Recorder from the Excel user interface, go to Tools -> Macro -> Record New Macro. If all you need is the formatting, pick one cell for each type of formatting that you need to perform. When you are done doing the formatting, press the Stop button to stop recording, or go to Tools -> Macro -> Stop Recording. Go to the Visual Basic Editor to view the code that the Excel Macro Recorder wrote to perform the formatting. This gives you the syntax needed for your code. If you always have the same number of rows, etc., you can just do all the formatting and then save the macro to run in the future. If you need to customize it, the next couple of examples show you how.

    Determining how to refer to your range becomes tricky. For example, let’s assume that you want to format a column as currency ($0.00). If you are certain that you want every cell in that column to be currency, you can set your range to be that column. In Excel, there is a property calledColumns that is available on aRangeobject and aWorksheetobject. If your range is already defined in a variable calledxlrng, and you want to perform formatting on the third column, refer to this column asxlrng.columns(3). If you do this, I suggest having an additional range object that you can refer to. If you have a range object calledxlrng2, you can write a line that saysset xlrng2 = xlrng.columns(3). I like to do this so that the range properties and methods will be available as I write the code.

    To explain that a little, if you have a worksheet that is currently active that you refer to as ActiveSheet, you can writeActiveSheet.Range("A1").Value=100, and it will put 100 in cell A1. However, as you type, VBA will not help you. If you sayset xlws = ActiveSheetand typexlws., as soon as you type the . you will see all of the properties and methods available. Using this technique for your objects will make coding much easier. If you have ever tried to script an Excel worksheet in VB script, you can appreciate how useful the help is while typing.

    In addition to using theRANGEobject, you can also use theWorksheet object to refer to a column. So, if you have a variablexlwsthat is defined as theActiveSheet, you can refer to column B by writingxlws.columns(2)orxlws.columns("B:B"). You can also refer to columns B through D by writingxlws.columns("B:D"). If you do not want to refer to entire columns and the range with your data has not been defined yet, you need a method to refer to this set of data. The following example will show you how to select from cell A1 to the end of the worksheet that has data in it.

      Public Sub SelActiveRange()
      Dim rng As Excel.Range
      Dim xlws As Excel.Worksheet

      Set xlws = ActiveSheet

      Set rng = xlws.Range(xlws.Range("A1"), xlws.Cells.SpecialCells(xlCellTypeLastCell)) rng.Select

      Set rng = Nothing
      Set xlws = Nothing
      End Sub

    The first thing you will notice is that you can refer to a range by using the A1 notation. So, to refer to cell A1, you can writexlws.Range("A1"). You could also do this by writingxlws.cells(1,1)—thecells collection also returns a range object that refers to a single cell.

    More Database Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Integrating Excel and Access," published...
       · Very simple and very pratical.André Luiz Bernardesbernardess@gmail.comSantos...
     

    Buy this book now. This article is excerpted from chapter three of the book Integrating Excel and Access, written by Michael Schmalz (O'Reilly, 2005; ISBN: 0596009739). Check it out today at your favorite bookstore. Buy this book now.

    DATABASE ARTICLES

    - Building Applications with Anonymous Types
    - A Closer Look at Anonymous Types
    - Programming with Anonymous Types
    - Converting Your Excel Worksheet into a Worki...
    - Excel Reference
    - Database Programming in C# with MySQL : Usin...
    - Formatting Techniques for Data Access from E...
    - Data Access from Excel VBA
    - Generating a Multiple Table Crystal Report u...
    - ADO and the Command Object
    - On Wiring Up an ADO Data Control
    - Reading and Writing to Files on the Intranet
    - Using ADO Record to Create and Navigate Intr...
    - Using Data Access Pages to Access Data on a ...
    - Using ADO with the SQL Native Client





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek