Copy file from folder based on string in name - excel

I have just started vorking with vba, and am slowly getting the hang of it.
I have a question as to the posibility of something:
I have an Excel sheet where I have part of the names of a lot of text datasheets I need to copy.
My folder destination is the following:
Same folder/Name from column F/Name from column B/text files with the string x in the name...
Is it at all possible to copy files like this from one folder to another?
I am thinking a for loop and a bunch of if statements?
Best Regards
Lonnie

To build on Alex's answer, and to confirm what you suspected already, you need to loop through all the files in the originating directory using the Dir command (this tutorial is a bit more readable than the msdn one: https://www.techonthenet.com/excel/formulas/dir.php)
then decide whether to move the file based on a rule (see here for string manipulation: http://www.excel-easy.com/vba/string-manipulation.html)
Finally, if the conditions are fulfilled, use the .copyfile command that Alex suggests in the comments.

Related

Version control on excel files in git

I'm looking for a way to perform version control on excel files in git. I understand this question has been asked before, but I couldn't find an option that will work for an excel file with multiple
sheets in it.
For instance, in the suggestions offered here, one way is to save .xlsx file as a csv file and use git diff. However, this option doesn't work when there are multiple sheets. I'll be able to save only the active sheet as csv file.
The second option that has been suggested on SO is GIT XL. From what I understand from the documentation
of GIT XL, the diff option will allow me to view the lines that are altered in the command prompt.
For example, if there are 10 columns in an excel sheet and I replace the value in all ten columns
how can I view the data that was present previously. To give a better context, the contents of
the sheet in excel is overwritten every time I run a script for different values of parameters.
In such a case, I don't want to re-run the script each time if I want to view the results of a previous run.
The third answer that I found useful is available here, using xls2text. Again, I would like to know how this will work when there are multiple sheets.
Any suggestions will be of great help!

Copy rows depending on cell value from different workbooks in multiple folders

I want to automate a process. I have multiple workbooks in different subfolders locally. I want to import all rows with a specific cell value to the MasterWorkbook.
How can I do that?
Could you provide some of the code that you've already done so you can be assisted better? the logic you would employ in your code would be:
Loop through all files in a folder and subfolders
In the loop, do the following for each file
Open file
Loop through the column that contains the cell value you want to check
If the check is true then copy the row to the main file
close file
And this process will continue until all files have been checked. All the above type of code can be easily googled or found on this site. You might want to also look at finding the lastrow for each file that you open to make the code dynamic.

Is there a way to make a logbook in excel of files being created?

I want to make a dynamic excel sheet that updates whenever a file is being created into a folder, think of letters. So the sheet needs to extract the name of the letter, the date&time when it is made, the author and so on, into a table in excel. Does anyone knows how to get that filedata?
I already know how to put data in a certain cell using VBA but the thing i get stuck in is how to get that data from a certain folder.
As Ron said, there is no event in VBA to handle that. However, if you want to stick to VBA only, you have basically two options:
You can update your cell whenever the workbook is opened using the Workbook.Open event (https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.open). This is not really dynamic but might be sufficient (depending on your needs).
Other option would be to call a function periodically to check if the folder content has changed (https://learn.microsoft.com/en-us/office/vba/api/excel.application.ontime)... Not very performant, but would work for sure.
For the file data just follow Ron's suggestion.
Hope that helps, MJ.

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?"

Resources