How to compact the VBA-project in an Excel workbook - vba

I'm am searching for a way to compact the VBA project in an Excel workbook.
I recently discovered that the VBA project in one of my workbooks had grown over 60 Mb when I opened the workbook with WinZip.
The compressed workbook (.xlsm) exceeded the 10Mb limit that my email provider has set for email attachments. I had to take action or find another way to distribute.
I was able to reduce the size from more than 10Mb to less than 2Mb but that was a lot of work and I'm not certain if we can automate this.
This is the procedure I followed:
Export the code behind all sheets, ThisWorkbook, Forms, CodeModules and ClassModules.
Save the workbook as .xlsx (All code is removed)
Reopen the workbook and save as .xlsm
Import all modules again and past the code behind the sheets.
Now that I know that the VBA project grows without an option in Excel to compact it with a single press of a button, at least not an option that I know of. I would like to create some tool to do this every time I am to distribute the file.
Any ideas how the do this?

I have had good experiences with this tool:
VBA Decompiler

Related

Running a WinForms application using Excel VBA

I have written a WinForms application in vb.NET using vs 2017. The application opens an excel Workbook which contains a document register, and updates the files as necessary.
I am very happy with the application, however one possible improvement I would like to pursue would be to attach the application to a macro in the Excel worksheet that the application updates, as the users of the application tend to open this worksheet by habit as it is.
I have the following code, which opens the application from the document register:
Sub AppMacro_Click()
Dim AppOpen
AppOpen = Shell("C:\Document Updater.exe", 1)
End Sub
The user can then run the application.
The problem with this method is that the application checks that no user has the workbook open, so that it can open the original copy (workbook is on a network drive), so if the user runs the macro, they will then have to close down the workbook before clicking the run button on the application. I would prefer if this didn't have to be the case.
What I would like to achieve is the following:
1) If the user runs the application using the macro, the application adapts and updates the opened workbook (as long as it isn't read-only)
2) If the user runs the application as usual, it still detects that no other user has the workbook open
What would be the best way to achieve this?
In general storing a workbook on a shared folder to be accessed/modified by multiple users is usually asking for trouble, you are better off using an Access DB if your users pool is fairly small or use full grown SQL (mySQL, SQL Server). Also since you have a working application in .NET where the user is very dependent on Excel, I would recommend you transition to an Excel Add-in. It is really easy and your solution would be more integrated with Excel

Importing from multiple workbooks

I have completed some code to open multiple workbooks and copy data into one master workbook. I can't figure out how the code "chooses" which workbook to open first to import. Anybody have any ideas what is the method excel uses to select the files?
Take a look at this post over here. There is a link in the responses to a blog which talks about Window natural sorting method. Unless you specify in the code which files to open, VBA will normally open the files based on the natural sorting method.

Executing a macro in another workbook

I'm doing an internship as a student developer and I have to program something that will automatically analyse spreadsheets every month.
I will have an Excel file that the users will open, and after typing the name of the files he wants to analyse and compare, generate graphs and other statistics.
Completely new to VBA (I only learned C#, PHP and Python at school), I figured out how to open a new Excel file with workbooks.open, but running a macro after opening a file won't work.
I guess it may be because my macros aren't recorded on those other files, but I need my program to work even after I'm gone, and I can't tell my users to copy the macros into new files every month.
I'm also looking into passing variables between workbooks, since for statistics purposes, I will have to retrieve, for instance, the value of cell G22 in every file opened and copy them in my primary file in order to make a graph out of those values.
Can someone point me in the right direction?
If you make a function or sub public you should be able to call it from another workbook, provided both are open.
public sub testMessage(input as string)
msgbox input
end sub

Can you schedule periodic data imports in VBA?

I have a VBA function that, when invoked via a button on a sheet, extracts a .csv file from a website and imports it into my workbook. The problem is the file is uploaded in different time intervals during the day, so I have to periodically check the website.
Is there a way in VBA that I can schedule when a function is invoked?
You can use the Application.OnTime() method which will allow you to schedule a Macro for a specific time amongst other settings. However you're probably best using VB Script or similar.
It's also important to note that the workbook would need to be open the entire time if you are going to use VBA, whereas a VBS file will run silently and can be triggered to run at startup.
If you're proficient enough with VBA then you shouldn't have a problem writing VBS.

VB.net text -> Excel conversion (with extensive formatting required after conversion)

I'm creating a program in VB.net that does the following:
At a high level I receive a file in email, put the attachment in a monitored folder, import the text file to excel, format the excel, and then email the excel file to a list of recipients.
Here is my plan:
Completed: Outlook VBA to monitor all incoming email for specific message. Once message is received drop attached .txt file in a specific network folder.
Completed: (VB.net) Monitor folder, when text file is added begin processing
Not Complete: (VB.net) Import text file to Excel
Not Complete: (VB.net) Format Excel Text file. (add in a row of data, format column headers with color/size, add some blank columns, add data validation to some of the blank columns that allow drop down selections)
Completed: (VB.net) Save file.
Completed: (VB.net) Send file to list of recipients.
Obviously the items above that are not complete are the bulk of the work, but I wanted to get some advice on what some of you think would be the best way to approach something like this. The import and formatting of the file are causing me some problems because I just can't decide what would be the most efficient way to do this.
What I've thought of so far:
The way stated above. Import to excel -> format
Having a template excel that contains all of the formatting already done for me and attempting to transition the data to this document (no clue if/how I can do this). Is it even feasible? Have the template already created and then import the text file to a new excel file, then transition that data to the excel template?
Something I thought about, in terms of formatting the document, was to record a macro of me doing all of the formatting that I'm going to need and then attempt to convert that macro into my vb.net code, but I'm not sure if that will work. I will need to verify that the text file comes in the EXACT format every time correct?
I really appreciate any advice/suggestions that anyone is willing to give.
You will want to use http://epplus.codeplex.com/
It allows you to create an Excel file from scratch, without having to start Excel itself. Automating Excel will make the process slow and it lacks robustness (Excel process can hang or not close properly).
In addition, using a .Net library allows you to run it on a server or so where no Excel is installed. (Next step would be to inspect the mailbox via POP, IMAP or the Exchange API, so that part doesn't have to be run on a client machine either)
http://msdn.microsoft.com/en-us/library/kh3965hw(v=vs.100).aspx
You can also just use the Interops from MS to interact with Excel, Outlook, Word, etc. They're not difficult at all to use. I'm not familiar with CodePlex, so that may be a better route or an easier one. I just wanted to provide you with an alternative.
With Microsoft Office 2010 Interops you can not generate Office files from .net applications anymore.
You can manipulate data from existing Excel files so you need templates(your 4th point). Then Excel allows you to query some databases. You may be able to simulate one with your folder, otherwise I suggest to convert your .txt files into some databases. (3rd point)
If you do use an older version, you can crate your Excel files by loading them into an instance of Excel and manipulating them as you wish.
By the way I supposed your attached files would have some sort of format.
If you want to manipulate Excel files, I can recommand you the NPOI library found on CodePlex. It has several advantages over OLE-automation:
NPOI is not dependent on a specific Excel version.
Excel (or any other Office component) need not to be installed.
It is faster.
It works with both .XLS and .XLSX files.
We are using a third party software called excel writer. May not be what you are looking for becauseit needs to be license, but it is very fast and the clients does not have to wait for a chart or a data output. Because we have that tool, have not try anything else.