If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > How to script an exisiting Excel sheet into VBA?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-23-08, 09:52
Wim Venema Wim Venema is offline
Registered User
 
Join Date: Jun 2004
Location: Lichtenvoorde, Netherlands
Posts: 53
Question How to script an exisiting Excel sheet into VBA?

Hi,

I often use macros in Excel and have many sheets that I use as template to
fill-in automatically figures from a MSSQL database.
I want to integrate the templates into the macro itself.
This makes the deployment of the application easier by just updating one Excel add-in.
Does anyone know how automatically script an exsisting Excel Sheet into VBA (including, formulas, graphs etc)?
This will save me a lot of work converting the templates into VBA.
Reply With Quote
  #2 (permalink)  
Old 10-06-08, 17:07
savbill savbill is offline
Registered User
 
Join Date: Feb 2004
Posts: 533
The best approach to this is to separate your VB Procedures from the data and the templates, then refer to the template in the code as a workbook.Worksheet or use ActiveSheet ActiveWorkbook references in your code. What you will end up with is a standard Template that will be used to create each report, a code file may be an add-in, and your database which you are already referencing. You may have to do some jockying around depending on how you are calling the data if you are using GetExternal Data with MS Query. But the idea is to separate the code / data / display template into 3 parts. This will make it much easier to maintian your process and distribute it.

Here's an example of code reference to the target worksheet where the data will be written:
Code:
strTemplate = Thisworkbook.Path & "MyCoolTemplate.xls"
Workbooks.Open(strTemlate)
ActiveWorkbook.Sheets(1).Activate
Set targWS = ActiveWorksheet

' Write some headers
For i = 1 to 4
targWS.Cells(1, i).Value = "Fields" & i
Next
__________________
~

Bill
Reply With Quote
  #3 (permalink)  
Old 10-14-08, 21:07
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
Excellent advice, Bill! Same goes for data setup within Excel itself. Makes for much more flexible and powerful approach.
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On