Using Directories in Excel - excel

I'm trying to get some data out of excel files. Never used excel before but I understand VB. So I have a file with a button my boss implemented as well as his code he used. Here is the code:
(you can see the "Get Data" button in there)
My exact problem is that I don't know a lot about excel so if someone could help me figure out what is going on as well as figuring out how to correctly use directories that would be awesome! For more context when you click the button it gets all files in the directory and then the user clicks a file then excel follows a macro to get some data out of sed file.
Right now it returns nothing but there is 5 files in that directory?
Here is what the default directory looks like after edit:
And here is the files in my directory:

As #Tim Williams says ... Insert the back slash after the folder name
DirNow = Dir(Range("DefaultDirectory") + "\WFP*", vbDirectory)
EDIT
Basic troublseshooting
Does it it compile?
Looks like "UserFormDataa" - should maybe be "UserFormData"
Add Debug.Print DirNow after it's set and show us the display
If that's not right add line
Debug.Print Range("DefaultDirectory") to makes sure range is defined properly
Remove vbDirectory - unless you're going to handle processing files under the matching sub-folders

You are missing the terminal backslash on your default directory.
C:\Users\CUCCOMTT\Desktop\Excel Project\
Otherwise you're looking for files named
C:\Users\CUCCOMTT\Desktop\Excel ProjectWFP

Related

PowerBI: Importing data from mixed folder files

I got a question related to the importing different files from the folder into Power Query (Power BI). When I say different, in my case are .xslx and .txt files. Actually just one text file but it is important to be inside report. Excel files are and will be always consistent as it is shown down in a first picture only with date as a dynamic part, but inside are consistently structured, so I just have to put it into folder and hit refresh into Power Query and magic.. that works fine, but I got also that .txt file which has completely different information but still connected to the report (because there is a field of date/time inside with additional information). My question is how or what is good approach to have all these files inside one or more queries?
As you can see on the second picture (from PQ editor) in the content part on the last position is .txt file, which I "isolated" when right click on it and "Add as a new query", and then I need to do editing and so on. Is there maybe another approach to solve this? One problem I discovered is when I change path of the file, all queries are refreshed but not this one with .txt - even though I changed path completely in the Advanced editor. Simply gives an error. Has anyone idea how to deal with different files from one folder, assuming that you need all the files from inside?
If you do not want 2 folders, your approach for isolating the txt is appropriate. about refreshing issues: if you expanded the data by clicking combine, Excel must have created other queries and parameters ("Sample from....") you must change the path in those queries too.

How to change the "fullpath" property of References in VBA (Tools -> Refences)?

I'm sure that there are different ways to phrase this question, but that is the end result that I want to achieve.
So, I have a setup where code is written in C# and added as functions to Excel. It relies on having a specific .tlb file in the Tools->References that can be found in the VBA window.
While I was testing this, the .tlb file (and the rest of them) was on my local drive, but now that the project is working, I need to transfer it to a network drive. The problem is that I can't find any way to change the actual file (or filepath) that is being referenced - it's always looking at my local path.
I've tried a few things:
Followed the steps listed here https://support.microsoft.com/en-us/help/308340/how-to-check-and-remove-incorrect-project-references-in-the-visual-bas
Tried several VBA codes using the .References.Remove expression. This does not actually remove the reference from the list, it only unticks it.
I've tried to remove the file from my local drive (causing an Excel error that a reference has been moved, deleted or renamed - good) and then add a reference from the new location that I want. This resulted in one of two things:
1) If I try to add it manually - nothing happens, the existing reference remains unticked and nothing new is added (that I know of).
2) If I try to do it via .References.AddFromFile "filepath" expression it ticks the reference, if it was unticked (this does not make the external formulas work), or an error that a reference with such a name already exists, if it was ticked.
Recompile on the network drive with the following silly way.
Open the VBA editor
Go into each module
Insert a line (doesn't matter what you write)
Press ENTER
Remove the line that you've inserted
When finished, in the menu click Debug \ Compile
Source: by Andreas Killer
https://answers.microsoft.com/en-us/msoffice/forum/all/ms-excel-error-cannot-run-the-macro-the-macros-may/3f3106b2-ae60-4d21-ac94-67e54e605922

Copy embedded text file from an Excel sheet with VBA

I have an Excel sheet with an embedded txt file which I would like to copy into a certain target folder.
I used the following code which works fine if I use it from my computer:
'copy oleobject
ActiveSheet.OLEObjects(1).Copy
'paste to activeworkbook's path
CreateObject("Shell.Application") _
.Namespace(ActiveWorkbook.path) _
.Self.InvokeVerb "Paste"
However, some of my colleagues did receive the text file which was embedded in the Excel sheet with an additional line beforehand which included the file save path in the temporary folder.
I could just delete this line with VBA but I would like to understand why this happens with other computers and not with mine.
Please help!
Thanks
OK so here is the issue:
txt file is a template and VBA is supposed to read it in again in a string file and change it. Until now it was just saved in a different folder and that worked fine but for mobility reasons (to be used by many other users) it would be a great advantage if it could be handled with just one file.
It is possible to do this with txt file embedded in Excel, but embedded files are not really well-supported by VBA automation. Most files only have a few methods available, and if I remember correctly with TXT file type, the available method is to open the file.
ActiveSheet.OLEObjects(1).OLEFormat.DoVerb 1
I mentioned a similar problem that I had (in PPT, instead of Excel, but the issue is the same). The route we chose initially was given here:
Extracting an OLEObject (XML Document) from PowerPoint VBA
Invoke the .OLEFormat.DoVerb 1 to open the file in Notepad
Use WinAPI functions to read the contents of Notepad into a string
variable
Use WinAPI functions to close Notepad
Use FileSystemObject to write/modify a new text file
Embed the new, modified text file
The functions to find and read Notepad contents are documented here and require some use of WinAPI functions (all noted in the link):
http://www.excelforum.com/excel-programming-vba-macros/729730-access-another-unsaved-excel-instance-and-unsaved-notepad-text.html
This is a LOT of work for not much benefit, instead you could simply open a file dialog prompting user to choose the text file (located on a shared network drive, etc.). This would be much more reliable.
Alternatively, depending on the size of the text file, you can store the contents inside a Shape (and you can put the shape on a hidden worksheet, etc.), either in the .TextFrame or another property that allows text. Ultimately we abandoned the solution in the links above in favor of storing the contents inside a Shape's .AlternativeText property. This worked very well for us, in storing XML contents of about 500,000 to 1 million characters per file.
The reason we chose not to go the Notepad route is that there was a lag while the file is being read and user could accidentally interrupt the procedure, corrupting the file(s), etc. and that Notepad doesn't fully support automation, etc.
I also thought of using a text field but if the oleoobject would work I thought I could also embed other file types like pictures.
Pictures are easy to work with because they have a built-in .Export method.

Matlab: open files 'outside Matlab' by default

I'm looking for a way to have Excel files in my Matlab folder open 'outside Matlab' (i.e., by MS Excel in most cases) directly by double-clicking the file, rather than right-clicking and selecting 'Open Outside Matlab'.
The .xls files reader built in Matlab can be terribly slow for large files, and an unwanted double-click on a file can cost quite some time in which Matlab is unresponsive.
Thanks.
When you click something in the Current Folder tab, it's actually running the open command, which itself calls finfo to determine what it means by "open" for a given extension. You can see this by creating a breakpoint in open.m directly after the line [~, openAction] = finfo(fullpath); and double clicking - when it hits the breakpoint you'll see it returns openAction as uiimport.
In theory, you can create custom methods for extensions by creating on the path a function openabc where abc is the extension, which should be returned as the openAction.
However, if I look at my finfo.m it first searches for said functions and then regardless of whether or not it finds them if there is an inbuilt method it overwrites them with the standard behaviour. There's even a comment:
% this setup will not allow users to override the default EXTread behavior
If you are willing to muck about in the inbuilts, you may be able to do it like this (backup first! - this could affect other things). I did it temporarily by shadowing the existing finfo like this:
edit finfo.m (Now save a copy to the current folder)
Add these lines after the loop that defines the openAction (in my version, around line 85):
if any(strcmp(['.' ext], matlab.io.internal.xlsreadSupportedExtensions))
openAction = 'winopen';
end
From the folder containing your edited finfo.m, type which finfo -all. You should see two copies, the MATLAB one labelled as shadowed. Opening something from the current folder window should now open Excel externally.
I don't believe there's any straightforward way to do that. It's built in to MATLAB that Excel files will open in the import tool when you double click on them, and there's no way to change that.
You might be able to get around it by changing the file extension on your Excel files to something other than .xls or .xlsx. That would stop MATLAB from opening it in the import tool. Then in Windows, you could associate the new file extension with Excel.

Excel form lost path

In my file server, some excel files lost their path,
Example:
=if(A2='\\172.168.1.1\SHARING\LANAIM\Gest_Qual\05__REG\04__Gest\[file1.xlsx]data'!$A2;2;False)
what happens and stay that way:
=if(A2='\\172.168.1.1\04__Gest\[file1.xlsx]data'!$A2;2;False)
How can I solve this problem? if someone can help me.
Thank you
You could have a hidden (or not) worksheet with the names of the files that it is to link to. Then use a VLOOKUP to handle the names. That way you can changes the file names in the lookup table and the links will redirect as needed.
Someone may have answered "yes" to update the links as some point and that caused them to change.
----EDIT----
I just thought of something:
Rename \172.168.1.1\SHARING\LANAIM\Gest_Qual\05_REG\04_Gest\ file1.xlsx
to file2.xlsx
Open \172.168.1.1\04__Gest\ file1.xlsx (Create it there if it does
not exist)
Open the file that has the links in it.
Verify that changes to file1.xslx appear in the linked file
You should now have 2 files open (the linked file and the dummy file1.xslx).
Use "Save As" to save file1.xlsx to the location that you want the links to point to (\172.168.1.1\SHARING\LANAIM\Gest_Qual\05_REG\04_Gest\ file1.xlsx).
Close file1.xlsx then close the linked file.
Rename \172.168.1.1\SHARING\LANAIM\Gest_Qual\05_REG\04_Gest\ file2.xlsx
back to original file1.xlsl (deleting the dummy file1.xslx in the process).
Open your linked file and the links should now point to the correct
file1.xslx.

Resources