Automated Excel document creation using VBA - vba

The managing director at our company wants me to produce an automated monthly document that saves to a certain place on our system so that he doesn't have to manually input all of the data. I have set up so that the document can save to the correct place in the correct format but my knowledge of VBA is not great.
Tackling this from a 1 question at a time point of view I suppose my question would be is it possible to create 1 very long macro that will accomplish many different tasks over several workbooks. For example we have a report that comes from our ERP (Baan) and shoves all of the data into one cell. Is it possible to create a macro that will accomplish formatting text to columns, then copy data from a cell based on a row reference and then take said data and paste it in to a different workbook? Would it then be able to save the workbook all from just running one macro and if so how long will all of that take once the macro is executed?

Yes I believe this should be do-able, keeping in mind that the file names + location remain the same (otherwise you'll have to edit each month). Create different Subs/Functions and call them in one main macro.
The easiest way is probably to do it step by step. Record macro's and see whether that already helps you out and if not use google & stackoverflow for help! :)

it is entirely possible - but in my Opinion VBA is not well suited to the task. The editor is atrocius at best and it is easy to produce highly specific "spaghetti code".
File operatione are possible, but are not nice. Error handling is 80s style with lots of goto.
So if you want to build something maintainable, build an external Application using Interop or epplus (.net package for reading /writing to excel documents) or an .net addin for office.

Related

Is it possible to create Excel VBA Code Library that several workbooks share?

I have several workbooks that have similar and reused VBA code sometimes I need to update the code and I would like to do this in one place instead of in 20 different workbooks is this possible?
If not is there a way to copy all the macros from one workbook to all the others after a change has been made?
Yes, you can reference other workbooks from VBA. Might be easiest to put it in an addin though.
If you are using class modules, then you can only set them as either private or public not createable (something like that). Obviously they'll need to be public, but you can get around the inability to create them by building simple helper functions in a normal module that do nothing other than create a new instance of the class and return it.
It is possible, yes.
This answer on SU mentions one possibility that is explored more in-depth in this support article.
If you find yourself recreating the same macros, you can copy those
macros to a special workbook called Personal.xlsb that is saved on
your computer.
By default, when you create a macro in Excel, the macro works only in
the workbook that contains it. This behavior is okay as long as you
don’t need to use that macro in other workbooks. Any macros that you
store in your personal workbook on a computer become available to you
in any workbook whenever you start Excel on that same computer.
In short: record a macro and choose to save it in Personal Macro Workbook. Save and exit, then re-open Excel. Go to the View-tab and click unhide.
The support article gives a more detailed step-by-step.
Sure is possible,
the two ways that I know are:
copy your macros on Personal.xlsb (like Vegard wrote) or (it's my usually case because I've also my custom Ribbon to play all my custom cmd) you can open a new excel file, write in this file all your macro/function/class.... and then save it as xlam file.
After you need to go on excel option, add components and choice the xlam file before created.
Tips, in the xlam file use a specific macro names and not macro1, macro2,.... because into your next files will be easy to create more macro and use the same name is usually..
I'll add my answer based on experience as it seems the ones given are all (unspoken) focused on being "free".
I started out using a workbook as my code library (as suggested above), but every time I made an adjustement or improvement to those code snippets, I had to go back to the other workbook and update it etc. This got old fast.
Once I started developing a bit more professionally, it made sense to invest in a tool to manage my code.
I use MZTools which also comes highly recommended by various Excel MVPs such as Ken Puls, Ro Bovey, etc. but there are other ones out there. Usually these tools also provide addtional functionalities that are useful to a developer. Invest a few bucks if you want to save your self from a headache.

Distribute updated Excel VBA code to Multiple End-Users

I've created an Excel 2010 workbook with a number of sheets. VBA code that performs various data manipulations is in a couple of modules, and also attached to a couple of forms. The workbook is being distributed to a couple dozen people in different departments to use. They will be populating their workbook with their own department-specific data.
If I need to distribute an update to the code (either a bug fix or some new function), how can that be done? I don't want the users to have to reenter or copy/paste all their data into the 'new' workbook - I'm essentially looking for a method to update the VBA Project that's inside their existing workbook.
You could create an additional helper workbook Help.xlsm, that has its file attributes set to read-only.
Move all the vba code, that you might need to change in the future, into Help.xlsm
The file that you distribute then needs a reference adding to Help.xlsm
Now in the future changes can be made to Help.xlsm and they should appear in the client's files.
Above assumes all your customers are on the same network so that you can store Help.xlsm somewhere that is accessible to all their files.
This article explains it better than me:
http://www.excelguru.ca/content.php?152-Deploying-Add-ins-in-a-Network-Environment
You would need to export the modules and forms and manually import them into the existing workbooks. I used to have to do this for some projects i worked on.
Alternatively you would need to write some helper code to import the old data into a newly published workbook, but this depends on how the data is organised of course. Again this is another approach I took for a different project.
You can also do this procedurally. Ive used this for small patches.
http://www.cpearson.com/excel/vbe.aspx

Using content of a specific cell to determine in which folder an Excel spreadsheet is saved

I have an Excel template that I use to keep information about various clients. These clients can be in various stages of the process such as "submitted", "pending", etc. The status of the client will determine in which folder the information spreadsheet is saved (i.e., there's a folder for all "pending" clients, a folder for all "submitted" clients, etc.). I was wondering if anyone knew if there was a way to write a Macro that could "read" information from a 'Status' cell and, depending on what was saved in that cell, save the Excel sheet to the correct folder?
I agree with the comments: the macro recorder is the easiest way of learning the syntax of commands that you can use from the keyboard. However, I do not believe it will help here.
When the macro checks the status cell, it needs to close the client workbook (I assume it has already been saved) and then move it from its current folder to the new one. I do not believe there is any way to get the macro recorder to record the statement that moves a file from one folder to another.
Small, easily written, macros can be used to perform a wide variety of tasks. You need to have some pretty advanced requirements before Excel VBA will not be adequate. I recommend everyone learns VBA. Search the Internet for "Excel VBA Tutorial". There are many tutorials and most are free. Try a few then complete one that matches your learning style. Alternatively, visit a good library or bookshop where you will find a selection of VBA primers. I recommend visiting the library and trying a few primers before visiting the bookshop to buy the one most suited to your needs. Learning VBA will not take too long and the investment in time will quickly repay itself.
Your macro will have to be in a master workbook which accesses and updates the client workbooks. Many of the answers here show how to work with multiple workbooks. Try a few search terms and I am sure you will find something that gets you started.
Look up "FileCopy Statement" This is the simpliest way to copy a file from one folder to another. The Move method which involves use of FileSystemObjects is more complex but more flexible.
Good luck and welcome to the joys of programming.

Manipulate Excel workbooks programmatically

I have an Excel workbook that I want to use as a template. It has several worksheets setup, one that produces the pretty graphs and summarizes the numbers. Sheet 1 needs to be populated with data that is generated by another program. The data comes in a tab delimited file.
Currently the user imports the tab delimited file into a new Workbook, selects all and copies. Then goes to the template and pastes the data into sheet1.
This is a large amount of data, 269 columns and over 135,000 rows. It’s a cumbersome process and the users are not experienced Excel users. All they really want is the pretty graphs.
I would like to add a step after the program that generates the data to programmatically automate the process the user currently must do manually.
Can anyone suggest the best method/programming language that could accomplish this?
POI is the answer. Look at the Apache website. You can use java to read the data and place it in cells. The examples are very easy.
You can can solve this, for example, by a simple VBA macro. Just use the macro recorder to record the steps the user does manually now, this will give you something to start with (you probably will have to add a function to let the user choose the import file).
You said you have some data generated by another program. What kind of program? A program that you have developed by yourself and where you can add the excel-import functionality? Or a third party program with a GUI that cannot be automated easily?
And if you really want to create an external program for this task - choose whatever programming lanuguage you like as long as it can use COM objects. In .NET, you have the option of using VSTO, but I would only suggest that for this task if you have already some experience with that (but than you would not ask this kind of question, I think :-))
Look here:
Create Excel (.XLS and .XLSX) file from C#
There's NPOI (.NET Framework version of POI) so that you can code in C# if you want.
If you use two workbooks - one for data and one for graphs - and don't update links automatically you can use a macro to get the data (maybe an ODBC connection if the file is in a format it can read - long shot) and then link the charts to the data workbook.
Use a macro to update the links and generate the charts and then send them out and hope no one updates the links.

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format.
I am currently doing some pre-development work for a project, and in most ways I would like to use SSIS for the project. The one area that my research and googling is leaving a big question mark over is the import of the excel sheet.
In short our users are using excel as a simple calculator / application. Values are entered into specific cells in the sheet and then in other cells formulas come up with the final answers. The sheet has been "beautified" to make it easier for users to use (i.e. white space, merged cells, pretty colours, etc). I need a mechanism to get both the raw values and the final answers and import them into a SQL database.
There is a ton of information on the trials and tribulations of importing column and row info into Excel, but have any fellow stack-overflowers had experience with this? Is SSIS appropriate for this (the other viable option would have to be integration into a .NET service).
Thanks!
What about adding a sheet to the workbook that pulls the answers from the calculation sheet and shows them in a tabular format, and then use the magic of SSIS to suck in the values from that new sheet?
If you can't modify the worksheet, you might want to look into Interop services or something where you can specify the exact cells you want to pull data from.
I'd try the first route if possible, though another option might be to simply recreate their Excel calculator as a .NET app...
A SSIS Excel data source uses worksheet or range -- think table with column names. You could also consider using VBA from within Excel to push data into your DB.
SpreadsheetGear for .NET will let you load an Excel workbook, put values in cells, calculate, get results as raw values or formatted text and more. This might be your best option if the data is not suitable for SSIS - but it will require that you use .NET (C#, VB or any .NET language) to automate your process. The advantage of SpreadsheetGear is that you then have a solution which does not depend on anything other than .NET (of course it depends on SpreadsheetGear, but SpreadsheetGear can be deployed royalty free with your application using xcopy deployment or any other deployment method).
You can see live samples here and download the free trial here.
Disclaimer: I own SpreadsheetGear LLC