Writing tab delimited line to text via VBA - vba

I have an issue I'm working on in which I could use advice.
I wrote a VBA program that takes one worksheet and expands each row in it to many rows on a new sheet, altering one variable in each row and appending necessary xml tags. It uses this to build up data on the second sheet which is then saved as a tab delimited file. This approach works great, and I've been using it for a while now.
I've started to get big enough data sets to hit memory issues though, so I'm thinking I need to change the process and want advice on the best approach.
My initial thought is that instead of generating a new sheet, I'll read in the input sheet and then generate an array of strings separated by tab delimiters which I'll then append directly to a text file. How do I do this fastest and without hitting memory issues? Is it better to build an array and write it line by line to the text file or to write each cell as I generate it and append the tab? Will I hit memory issues? Will saving the file partway through help?
Any suggestions or alternative approaches are much appreciated.

Related

Wondering if it's possible to create an XLSX with freezed first row?

I am trying to use SheetJS to create a simple Excel file from our Angular (17?) web app.
It is working very well but, now I got greedy and I want to fix the first row ("freeze row" in Excel which is a very popular method of freezing the headers row when scrolling the actual data up and down).
Is it possible?

Is there a way to find the page break locations in an xlsxwriter worksheet?

In my code I generate a worksheet that uses fields with text wrapping, so I don't know exactly how many lines will be on a page when xlsxwriter creates the worksheet. Due to limitations in the app into which I need to import the xlsx worksheet, I need to take my original worksheet and split it so that each page becomes a worksheet in a new workbook.
Can I somehow access the location of page breaks after running fit_to_pages(), or alternately, is there a way to know exactly how many rows will be used when you run text wrapping on a field?
Can I somehow access the location of page breaks after running fit_to_pages()
No. That isn't stored in the file format. Excel calculates that at runtime when it loads the file.
Excel also adjusts the height of cells containing wrapped text automatically at runtime. You could prevent this by specifying an explicit row height for the rows that contains the wrapped text so that their height isn't adjusted automatically. However, setting the height explicitly for wrapped text requires some sort of estimation, which takes us to the second part of your question.
or alternately, is there a way to know exactly how many rows will be used when you run text wrapping on a field?
The only way to do this with 100% accuracy would be to use some Windows API functions with bounding box calculations. I'm not sure if that is (non-theoretically) possible in Python and if it is it probably isn't portable.
So you are going to have to make some sort of compromise and use explicit row heights that will allow you to calculate the height of the printed page and thus the page break location but not have the nice automatically height adjusted cells for wrapped text.

VBA to create Mapinfo table & points

not very good at VBA, just in the basics so got stuck at below idea.
I want a VBA to create a MapInfo Tab file and points with Legend from a table in excel with given coordinates and legend based on another column.
searched a lot online, but couldn't find more than the below.
CreateObject("MapInfo.Application")
Please guide me to accomplish this.
Thanks
There are two ways of doing this.
First option is to open MapInfo application (with CreateObject("MapInfo.Application")) and create TAB file there. However, this would be basically to write an MapBasic program, VBA would be only used to start MapInfo.
The second options is to create a MIF/MID file. These are two plain text files which can be imported into MapInfo. At this page Pitney Bowes states "We do publish the MapInfo Interchange Format (MIF\MID) files in our documentation", however you find it in Chapter 13 at normal MapInfo User Guide.
I would recommend the second option. Creating two plain text files in VBA should be feasible. A good starting point would be to create a TAB file manually and export this TAB file as MIF/MID, then you have a valid example file.

Passing an Auto_Open macro from one workbook to another

I have to pass an Auto_Open macro from one Excel file to about one hundred other Excel files, which are generated automatically from R-script. I really don't know the VBA code.
At first I hoped that opening the file containing the macro and then opening the rest of the files will do the work, but unfortunately it didn't happen and I assume I have to pass the VBA code to all the files.
This macro does a little bit of formatting and limits the values in some cells. I found similar subject but I had problems with modifying VBA code to solve my problem. Is there any way to do simply copy that?
One solution would be to create a template containing the "Auto_Open" Macro and use this template to generate the hundreds of files with the "R-Script"

Locking text fields in embedded document

I have been tasked with updating a spreadsheet that produces a report by replacing text in a template. Previously, the worksheet referenced an outside/separate file-- my job is to get it working in an embedded document.
I currently have text form fields for every input I want to insert within the embedded document template. As it stands, users can edit the document template however they like, but if they accidentally erase a text form field (again, where text is replaced via the vba macro) then it will break the macro and the spreadsheet will be useless.
My question:
Is there some way to lock or protect text form fields such that the rest of the document is editable? I essentially want to have the inverse of a "text form field only" protection.
Alternatively, is there a better way to approach this project? I'm thinking of leaving the spreadsheet as is, but including a "reset" button bringing the template back to it's original state if anything breaks. If I did this method, this would require there NOT to be an external file. Attempts to do this so far have proven unsuccessful.
My general methodology/algorithm goes as follows:
Open the reference document
Replace all the text form fields via bookmarks with plain text,
making sure to reassign the bookmarks afterward (so as to not lose
them if they run the macro more than once).
Save the embedded document as a .doc with the inputs inserted
Replace all bookmarked inputs with text form fields to return the
template to its original state
Any information would be IMMENSELY appreciated. I am slowly running out of time and am feeling stuck.
Many thanks!
-Sooji