Database
  Home arrow Database arrow Page 2 - Formatting Techniques for Data Access from...
Iron Speed
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
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 / 9
    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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Formatting Techniques for Data Access from Excel VBA - Formatting Techniques Continued
    (Page 2 of 5 )

    The second method is calledSpecialCells. TheSpecialCellsmethod returns a range object based on constants built into Excel. I usexlCellTypeLastCell, a special cell that refers to the last cell being used. Of the others you can use, two that I find particularly useful arexlCellTypeFormulas andxlCellTypeComments. These return a range that includes all of the cells that have either formulas or comments, respectively.

    Often, I want to send an Excel Worksheet to someone, but I don’t want the recipient to change something on one sheet that will affect formula results on another. For example, if you show a scenario of net income based on sales assumptions, and you do not want that net income scenario to be changed if the sales assumptions change, you can change all of the formulas to values using the following procedure. This allows you to maintain a history of the results at various stages. For example, if you make a copy of the scenario with one sales assumption and then remove the formulas, you can maintain the results while still having a second worksheet that will change when the sales assumptions change. Note that the following example assumes that your report is in cells A1 to D100. (You could also build that first range dynamically by using thexlCellTypeLastCellmethod used above instead of building it directly into the code if you needed more flexibility or if the size of your spreadsheet will grow larger.)

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

      Set xlws = ActiveSheet
      Set rng = xlws.Range("A1:D100").SpecialCells(xlCellTypeFormulas)
      For Each rng2 In rng.Cells
       
    rng2.Copy
       
    rng2.PasteSpecial xlPasteValues
      Next rng2
      xlws.Range("A1").Select
      Application.CutCopyMode = False
      Set rng = Nothing
      Set rng2 = Nothing
      Set xlws = Nothing
      End Sub

    This procedure uses aFor Each...Nextloop to loop through every cell in the Range variablerng. Each individual cell is copied and then pasted back onto itself with only the values. When it is done, cell A1 is selected and theCutCopyModeis set toFalse(if you do not set theCutCopyModetoFalse, cell A1 will be selected, but the last cell copied will still have the marquee box around it as if it is being copied). You could select the entire sheet and use Copy and Paste Special Values, or aFor Each...Nextloop can be placed into code and set to a particular range by changing it like this:

      Public Sub FormulatoConstant(targetrange As Excel.Range)
      Dim rng As Excel.Range
      Dim xlws As Excel.Worksheet
      Dim rng2 As Excel.Range

      Set xlws = ActiveSheet
      Set rng = targetrange.SpecialCells(xlCellTypeFormulas)
      For Each rng2 In rng.Cells
        rng2.Copy
       
    rng2.PasteSpecial xlPasteValues
      Next rng2
      xlws.Range("A1").Select
      Application.CutCopyMode = False
      Set rng = Nothing
      Set rng2 = Nothing
      Set xlws = Nothing
      End Sub

    UsingFor Each...Nextallows you to target just the range of cells that you want to find formulas in and change them to values. You could take this code, put it into any of your Excel workbooks, and with a simple call to the procedure, perform this action.

    More Database Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Integrating Excel and Access," published...
     

    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

    - 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
    - ADO`s Stream Object
    - Opening a Record Object Referencing an Open ...
    - Introducing Jasper (SQL Anywhere 10 Beta)
    - Creating a Database Project in VS 2005
    - Manipulating ADO Recordsets

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway