VBA loop through non-patterned files in folder (seek for opinions) [EDIT] - vba

I am here for seeking any advice or opinion as I want to loop through every excel files in folder. As you can see from my attached picture, my excel files are different both in file types (.xls <> .xlsx) and filename (especially on 2018). I also need to loop through "Revised" or "revised" files as well since it is possibly that any file will be revised next time.
And yes, I also did some research on this. My understanding is I need to modify all of the file names into the pattern one before build up a VBA to loop. At first, I thought about decomposing all filenames and put it back in pattern form, but it sound too idealistic. Another way is using the date in each file to label the workbook name, but again I found that those date had different styles. Some files label the date by using string such as "January 2012" or "March 2014", while the others using the date form such as "19/08/2013".
Therefore, I would be appreciated if anyone could suggest me on;
How can I handle with the different file name (.xls and .xlsx) within the same VBA?
How should I deal with these different file names (some files have "revised" at the back; some do not have "-" between "Cons" and date; and some use month name instead of number)? Are they any pattern that I overlook?
Please noted that I am just a newbie VBA coder, so it would be great if you left your answers with an explanation or any kind of examples.
Many thanks.
--------------------------------------------------------[EDIT]-------------------------------------------------------------------
First of all sorry for my poor explanation before. I provided too few information to understand overall picture. Let's start this over again.
My data are about steel consumption which release from the authority
every month. My task is to gather all of these data (such as
production, import, export and consumption of every data in each
row) and generate into time series pattern (please see attached
excel screen)
As it is possibly that these data will be revised anytime, I thus
decide to download all of these file every time in every month (one
file per one month). In addition to those revised file, the
authority will unexpectedly rename those file for example, from
"Cons 201601.xlsx" into "Cons 201601 - revised.xlsx)". This make me
more difficult to work on this (please see attached folder for
reference).
Moreover, this authority seems to have a problem with file naming as
they had different pattern of filename in the past compare to the
present ones. Example is per below table; Cons 201701-Revised.xlsx
Cons 201710-Revised1.xlsx
Cons 201711.xlsx
Cons-200902.xls
Cons-201212_revise.xls
Cons-201401-revised.xls
I mention above file name in order to create a VBA to loop through
these file, select some content and paste into another workbook in
chronological order. This means that I cannot use "Loop while or Do
while function" in my VBA. At first I decided to use two integer
variables, both of which were set for years and months
(e.g. For i = 2009 to 2018 and For j = 1 to 12) in order to created the system of filename (such as filename = "Cons" & "-" & i & j). But,
as I stated before, non-patterned name by the authority had
prevented me from creating this kind of loop.
I also tried to use the date in cell B2 in figure 1 to label the
date in order to create the loop which I already explained before.
However, again, the authority did not use the same pattern to date
month and year. After I checked with many file, these are example of
the date style in cell B2 January 2012 (string)
February 2009 (string)
Jan-16 (1/1/2016 date in custom format)
Given above limitations, could you guys again suggest me any possible
way to create chronological loop so as to copy and paste data to another
workbook to form a set of time series data for each product?
Thank you for your kind help :)

Firstly, use FileSystemObject (include a reference to Microsoft Scripting Runtime in your VBA project) which has some helpful functions within it. You could always code your own, but why reinvent the wheel in this case?
Don't have time to codes something this morning, so here is the pseudocode:
Open a Folder using your known filepath
Loop through all the files in the Folder (For each f in Folder.Files
extract the date code from your filename (e.g. using RegEx)
Add to a collection (e.g. array or Dictionary item) of the filename and the extracted date code (your key).
(end loop)
Sort your collection based on the extracted date code
This now gives you an ordered list of files, which you can open in turn and extract the data. An added bonus is that the key in the collection gives you a consistent date representation which you can use as an index in your collated information.

If you just want to loop through all files in folder use this:
dim file as variant
file = Dir("<PathToFolder>")
While (file <> "")
'Your logic here
file = Dir
wend

Related

Preserve settings in the file

I am creating my excel add-in that saves the current file as csv into user-specified folder. I would like my program to ask for the folder path the first time the program is launched and to remember that folder in the future. I am wondering is there any way to preserve the data within the program? I figured that I could write the path into .txt-file but that feels a little hack-like solution and would clutter the addin folder.
I use the GetSetting and SaveSetting functions in my VB 6 apps. Rather than cover them in detail, take a look at this excellent web page that illustrates how to use in with Excel
Excel Tips From John Walkenbach
Create a Worksheet, and store the values in cells. Then in the VBA Editor find the Worksheet in the Project Explorer (Ctrl + R) and set "Visible" to "2 - xlSheetVeryHidden" in the Properties Pane (F4) so that it is not readily visible to users.
You can then set/retrieve the data in with code in the format SheetName.Cells(row,column).Value, e.g.
MyPath = Sheet1.Cells(1,2).Value 'Get data from cell B1
Sheet1.Cells(2,2).Value = NewPath 'Set data in cell B2
There are multiple ways to approach this. Besides the hidden sheet approach, already described, you can
Write the information to a CustomXMLPart. This is a xml file stored in the workbook's ZIP file where information can be stored.
For something as short and simple as a file path, you could also use a CustomDocumentProperty

VBA: Code within INI file

I am currently making a Macro that can send out a bunch of emails with attachments all at once. I did this in outlook, but I'd like to redo it within Excel, so any user can simply press a button on the spreadsheet and it emails out the reports. My issue is certain reports have a special subject for example (Cabana Arrivals till (6 months from date)) in outlook I simply put dateadd("M",6,Date) after the subject my issue is I don't want to 'hard code' this within the loop. My question is when VBA reads the INI file, is there anyway to make the key's data active? So when it reads this key:
Special Instructions=format(dateadd("M",6,date), "MM/DD/YYYY")
the subject will now be "Cabana Arrivals Till 04/07/2016" or is there a totally different way I need to look at this? Any help is appreciated wasn't sure on how to even find the answer to this as I've never heard of doing it before.
Date add would be the only function used so create a key called 'subject date add' and set it equal to the interval (m,d,y) and the # of days/months/year. It should look like ('subject date add' = m,6) split on , and remove spaces for date grab the files modify date. If key does not exist skip.

Change sheet name in a link to another workbook

This is probably a very simple question but I am having trouble solving it.
Every month I need to update the data in a workbook based on what I have in another one.
In the source workbook the data is separated in different sheets for each month (Sheet name ex: Forecast Jan, Forecast Feb ...).
In the destination workbook, my links are pretty simple:
=+'Q:\ ... \ [CLH_2016_01 Displaced inventory 2015-12-14.xlsm]**Forecast Aug**'!$C$65
What I am looking for is a way to change Forecast Aug with Forecast Sep.
I have tried the easy way with Ctrl + H but I have over 4000 formulas to change and it takes a while (almost an hour if it does not crash before the end).
Thanks in advance!
you can use indirect :
=INDIRECT("'Q:\ ... \ [CLH_2016_01 Displaced inventory 2015-12-14.xlsm]Forecast "&TEXT(TODAY(),"mmm")&"'!$C$65")
However, this will work only if the external workbook is open, which might be a problem if you have several different workbooks.
In that case, the workaround I had done in my previous job was to use INDIRECT.EXT, a function that performs INDIRECT and is able to check in closed workbooks.
However, this function is included in Morefunc.xll which is an external library, so you will require an installation.
Based on the solutions given by #Siddharth Rout and #Jeeped, here are two ways of improving the process:
-Change calculations to manual and then try Ctrl + H
-Your Find & Replace will likely be much quicker if you open the external file into the same application instance. The extended time to update would seem to be from re-evaluating each change individually.

Write in specific point of a docx document using vba

I need to modify a template document (docx) automatically, according to info I've read into some text files and I'm using VBA for this.The template document has a predefined record, which looks like this:
description: first description which can take more rows
...
author: the author
date: the date
...
What I need is copying the previous line below the first one, as many times as many text file I have, and complete each one whit info I've copied into strings. So,for example, if I have 3 text file I'll have the previous line three times, like this:
description: first description
...
author: the first author
date: the date in the first file
description: second description
...
author: the second author
date: the date in the second file
description: third description
...
author: the third author
date: the date in the third file
where the first one is already there and I need to repeat it twice.
How can I tell Word to copy and write in specific points of the document?
I've thought something like the following:(The code probably is not correct, I'll put it there to explaying my idea)
For i = 1 To n(number of text file)
ActiveDocument.Range(Start:=ActiveDocument.Paragraphs(Index).Range.Start
End:=ActiveDocument.Paragraphs(Index+3).Range.End).Select
Selection.Copy
ActiveDocument.Paragraphs(Index+4).Select
Selection.Paste
Next i
the previous was for copying the record.
Now I have to fill that records writing the info after title:, author: and date: of each one, so something like:
For i=1 To n
ActiveDocument.Paragraphs(Index(i)+1).Words(2) = description(i)
ActiveDocument.Paragraphs(Index(i)+2).Words(2) = author(i)
ActiveDocument.Paragraphs(Index(i)+3).Words(2) = date(i)
Next i
where description, author and date are text string I already have.
Does that make any sense or I'm taking a wrong way? Is there any smarter VBA objects I could use?
Unfortunately, I don't have enough points yet to add this as a comment, where it would actually belong...
Which version of Word are you using?
Do you have any influence on the construction of the template, so that you could, for example, add bookmarks such as iDevelop suggests or possibly ContentControls (would be better)?
If you have no influence then it would be better for you to plan entering the data in each "copy" loop.
Note: I hope you're using Documents.Add to create new documents from the template so that you're not actually changing the template?

vb.NET SaveAs not saving all Excel data

I have a very strange issue that I cannot seem to find an answer to online.
I have a VB.NET application that creates an Excel of data (roughly 42,542 rows in total) and the saves the file to a folder location & opens it on screen for the user.
The onscreen version & folder version is only showing 16,372 rows of data like it is being cut off.
When I go through debug I can see all the rows are being added & if I save manually in debug all the rows save. Some data seems to get lost on the system save.
I am taking data from 4 record sets & writing each set one after the other with specific headers for each block on the Excel sheet.
My save line is:
xlWBook.SaveAs(Filename:=sFileName, FileFormat:=Excel.XlFileFormat.xlExcel7)
Would anyone please have any ideas as to what this might be?
Older version of Excel only support 16,384 rows per worksheet. You are saving as Excel7 (which is Excel 95) and has this limitation:
See here for a summary of sizes per version:
https://superuser.com/questions/366468/what-is-the-maximum-allowed-rows-in-a-microsoft-excel-xls-or-xlsx
Change your code to another format, See here for all the allowed formats: XlFileFormat Enumeration
However the file format is actually an optional argument in the SaveAs method, so you could leave it off altogether: "For an existing file, the default format is the last file format specified; for a new file, the default is the format of the version of Excel being used."
Source: WorkBook.SaveAs Method