Compare two excel VBA projects - excel

I am writing some VBA macros in Excel 2010.
Source code is managed with TortoiseHG.
However I cannot find a smooth way of comparing 2 different commits, because the TortoiseHG only shows the complete excelfile and not the different VBA files I'm writing my code in.
If anyone have done this in a nice and smooth way I'd be very happy to get some ideas.
Also using an external tool like BeyondCompare is possible.
Thank you!

For the VBA Code:
For code comparison purposes I'd suggest exporting the VBA module(s) and checking in the .bas (regular module) and .cls (class module) files and checking those in next to the .xlsm.
Those exported VBA modules actually give you plain text versions of the code. Code in Worksheet / Workbook modules can also be exported as .cls files. This will allow TortoiseHG to compare the actual code instead of the Excel file itself.
For the Excel file comparison on itself:
For comparing the actual XLSM, you can consider saving the file, changing the extension to .zip and extracting the zips contents.
An Excel file for version 2007 and up is actually an archive containing XMLs that define the whole workbook and a .bin file for the VBA Project.
You might want to pull said xmls through XMLLint or a similar tool, since they're not pretty printed by default.
Side note:
You're asking for a nice and smooth way - I'd say there is none within Excel itself. You can use VBA to do these Module Exports on Workbook_BeforeClose() event, but you'll run into some security issues - By default you're not allowed to access the VBProject from within.
Of course there are some 3rd party tools for comparing VBA code without having to extract the modules, however "what to use" will be severely opinion based - BeyondCompare is indeed one option.
Hope this helps.

Beyond Compare has an add-on file format to compare VBA code in Excel files. Download the Microsoft Excel Workbooks VBA file format from the Additional File Formats for BC4 page.

You can export each module, and compare using Windows native fc command.
fc /N Module1 Module2 > Result.txt
Help:
fc /?
Compares two files or sets of files and displays the differences between
them
FC [/A] [/C] [/L] [/LBn] [/N] [/OFF[LINE]] [/T] [/U] [/W] [/nnnn]
[drive1:][path1]filename1 [drive2:][path2]filename2
FC /B [drive1:][path1]filename1 [drive2:][path2]filename2
/A Displays only first and last lines for each set of differences.
/B Performs a binary comparison.
/C Disregards the case of letters.
/L Compares files as ASCII text.
/LBn Sets the maximum consecutive mismatches to the specified
number of lines.
/N Displays the line numbers on an ASCII comparison.
/OFF[LINE] Do not skip files with offline attribute set.
/T Does not expand tabs to spaces.
/U Compare files as UNICODE text files.
/W Compresses white space (tabs and spaces) for comparison.
/nnnn Specifies the number of consecutive lines that must match
after a mismatch.
[drive1:][path1]filename1
Specifies the first file or set of files to compare.
[drive2:][path2]filename2
Specifies the second file or set of files to compare.
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/fc

Related

What is the simplest Excel file format with formatting included

I create .CSV files by building the content like this:
s= "column1, column2, column3 \r\n"
s+= "R2column1, R2column2, R2column3 \r\n"
saveas("file.csv", s);
I now need to include a way to resize the columns when viewed in Excel.
I've read that CSV cannot do this, so what is the next simplest excel file format that can? And how would new syntax look?
CSV files are simple text files that contain plain data.
You can open these files in Excel, and it will be displayed in the spreadsheet view for convenience, with each field separated by the separator (in your case the ,) in a separate cell.
Although you can change the width of an Excel column, this is purely a visual style in Excel and can only be saved in an Excel file.
The solution would therefore be to convert your CSV files to Excel (*.xlsx) files.
Depending on the language you use you can probably directly create Excel files, without the need for conversion. There are libraries available for most programming languages for exactly that purpose.
If you want to create Excel files with Android, this might help: How to create an excel file in android?

Batch save pictures from multiple excel files in a folder

I have about 10000 excel files, that in a specific cell of all of them there is a picture. I need a script to read all files and save the picture with the same name of the excel files in a folder.
Could you please help with that?
Thanks.
This method is based on a number of assumptions:
All the files (10000) are located in a know folder,
All files are named according to a paradigm that can be reproduced programmatically (if not, you can get the list of files within the folder, store the list within an array, and loop through the array),
Pictures are always within the same worksheet or, if in more than one, the names of the worksheets can be reproduced programmatically,
The filenames to be used to save the pictures can match (at least as a seed) the one of the Excel the pictures are extracted,
You will manage to write some basic VBA.
Note that for the VBA you have at least two options:
Write it within an EXCEL that will only serve as the extraction engine, or
Write it as a stand-alone file and run it via DOS commands.
The VBA logic:
Create the outer loop that processes a single file,
Within the outer loop, generate the name of a file to be open,
Open the file using Workbooks.Open VBA function,
Select the worksheet and the cell containing the picture,
Use the Workbook.SaveAs to save the picture (you will need to specify the type of file to be used, e.g. .bmp).
As a simple and very efficient tool to get the code (at least) partially generated by Excel, you can RECORD a MACRO for each action and then stop recording. You will see the code generated (you will need to access the VBA mode). You can copy-paste the generated code into your development (you might need to do some simple adaptations though).
That's it.
Hope you manage. Good luck!!

Perl - Changing Excel Worksheet name

In a perl script I am writing, I am trying to find a way to open an existing excel spreadsheet, change the name of the first worksheet, and save it. It would seem like a simple task but I haven't found a simple way to do it.
Spreadsheet::WriteExcel can easily change worksheet name, but it seems like it can't read in an existing excel file.
Another constraint is that the perl module I use shouldn't need installation. I can work around this if there's no good option, but it would make things more complicated.
Edit: I am using ActivePerl 5.18, so modules included in this are ideal.
The only way of doing this while preserving everything else in the Excel file is to use Win32::OLE.
That requires having Excel installed on the computer on which the program will be run, and, of course, only works on Windows.
If you can't do that, you will have to read the Excel file, and write out the contents to another file, changing the name of the worksheet in the process. Depending on exactly what you have in the source Excel file, this can get rather involved rather fast.
See also "How can I merge two Excel (xls) files in Perl or batch?" and "In Perl, how can I copy a subset of columns from an XLSX work sheet to another?"

writing csv to open as different tabs in Excel

Is there some way to write a csv file such that , when opened in MS Excel , it will open in different tabs in the workspace ?
The short answer is, NO.
For that matter, the long answer is NO too.
csv is a continuous run of lines of values separated by commas. each line doesn't even have to have the same number of values etc. there's no concept of workbooks or different "areas" in csv. Excel cannot be cajoled into opening a csv into multiple workbooks...well at least not without writing VBA to parse the csv file yourself.
the oxml or whatever they've ended up calling the xml file spec for office, allows workbooks and is still easy to deal with being text based. Do you have to use csv or can you switch (at least part way through) to xml?

Writing a formula into an excel file using RODBC

I am able to write a data frame to an excel file, using the RODBC package.
Now I would like to include some formulas, e.g. =A1, which upon opening the excel file are interpreted as such; including "=A1" as text in the data frame results in a string entry "=A1" in the excel file (the value shown in the formula bar is '=A1), and is not interpreted as a formula.
You need to write your file without quotes. When I constructed a tiny file named testcsv.csv with this as contents:
=B2, 2
... And then used the File/Open ... menu and used the dialog to open it after selecting All Files as the file type, the expected calculation occurs:
(This is on a Mac with Excel2011, <\strike> so Windoze might be different. <\strike>) Works the same on Excel 2007 running in WinXP.
You are better off using one of the CRAN packages interfacing xls file natively -- I had good luck with xlsx; others have reported success with xlsReadWrite
Another possible approach in windows would be to use the rcom package http://rcom.univie.ac.at/.

Resources