ExecuteExcel4Macro & update values dialog box - excel

I'm currently coding a small VBA tool in order to quickly sort files according to some data inside. As an example, I have a "To be sorted" folder with some files in it, lets say "File1.xls, File2.xls" and so on.
To read data in "FileX.xls", I use ExecuteExcel4Macro (with the range and filename set properly), then I can use this data to apply filters and move files in other directories.
My code works fine with local files, but when I try to run it on my server (the tool aims at sorting files stored on the server), a dialog box "Update values" appears when I use ExecuteExcel4Macro.
From here, I have to options:
manually opening each file before running the macro (I have like 250 files to sort so...)
pressing cancel on the dialog box which causes the plugin to crash (type mismatch error)
Here is the portion of code that bothers me
For Each fsoFile In fsoParentFol.Files
'I only want to sort .xls files
If Right(fsoFile.Name, 4) = ".xls" Then
'On active le workbook actuel
strRef = "'[" & fsoFile.Name & "]" & "Sheet1" & "'!"
'this is the data I want to retrieve ... and this is where the dialog pops up
projectName = ExecuteExcel4Macro(strRef & rngProjectName.Address(1, 1, xlR1C1))
'some more code goes here but is irrelevant
End If
Next fsoFile
So my question is: how can I disable this dialog or, if it's a bad idea, are there other clean methods (I don't want to open each workbook, even with screenupdate turned off) that could solve my issue?
DisplayAlerts = False doesn't solve the problem. And again, the program runs fine without this dialog.
I've of course tried to research online my problem first before posting here, but even subjects like this one aren't solutions to my problem.

Change the UNC path to a path using the drive letter that the server is mapped to:
So for example, change your string to produce:
[P:\Some_Folder\myfile.xls]Sheet1!A1
Instead of:
[\\my-server\Some_Folder\myfile.xls]Sheet1!A1
You could try using the replace function to achieve this:
strRef = "'[" & Replace(fsoFile.Name, "\\", "P:\") & "]" & "Sheet1" & "'!"

I would use code more like this:
projectName = Workbooks.Open("\\srv1\folder\Book1.xlsx").Sheets("Sheet1").Range("a1")
Workbooks("Book1.xlsx").Close
This works with a mapped network drive too.

Related

Closing workbook with partial file name

I have vba that works great to open a file based of a partial criteria met. but when I have multiple files open the file opening by the below VBA remains open when done and I am having trouble how to call that workbook back to close it seeing it opens off partial parameters being met. in quick summary the opencopy (opens a file), then in another vba I copy that data, and pastes it into another workbook (done in another VBA) when done i want to bring that workbook back up and just close it. if i do activeworkbook.close it closes the workbook i'm working on which i don't want. Help im stuck!
Sub OpenCopy()
Dim sPath As String
Dim sPartial As String
Dim sFName As String
sPath = "C:\" ' <<<<< change accordingly
sPartial = "AAA_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) &
IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.txt"
sFName = Dir(sPath & sPartial)
If Len(sFName) > 0 Then
Workbooks.OpenText sPath & sFName
Else
MsgBox "File not found.", vbExclamationEnd If
End Sub
Your problem is in "the other VBA". Try to force your project to comply with the rules by which VBA works.
That would mean to acknowledge that there is only one VBA. That VBA has procedures. You can call these procedures manually (which you probably do, and which leads you to talk of "the other VBA") or you can create a Main procedure that calls them for you, one after the other. The advantage of this would be that you can declare objects and variables in the Main which could be made available to all procedures, passed as arguments.
So, you would open a file, finding it by an abbreviated description of its name. You would assign this file to a variable, say File1, and now you could refer to it from anywhere in your project to read or copy from and to close it. You never need to know its name because you refer to it by the variable name to which it was assigned. VBA will assure that the name is unique.
As an alternative, you could of course take the file's complete name from the file after it was opened and then close the file by its complete name. In fact, the Dir function in your code does provide the complete name. So you could take it from there even before the file is opened. As I said, the problem is that the name would be in "the other VBA", an idea that isn't supported by VBA.
But, presuming that you want to continue with your piecemeal approach at least for the moment, wouldn't you be able to identify the file by the same partial details that led you to find it in the first place? I presume that the names of files you have open at the same time would differ in the "AAA" part. So, If Instr(ActiveWorkbook.Name, "AAA") = 1 Then would identify a file. You could loop through all open workbooks with For Each Wb In Application.Workbooks and check each name. You may have to add an input box to enter "AAA" another time because your different "VBAs" can't be made to talk to each other.
By the way, your beautiful partial string could be simplified using VBA's Format function. Try this.
sPartial = "AAA_" & format(date, "yyyymmdd")

Using vba, is it possible to open multiple Excel workbooks from the same folder and keep them open at the same time?

This is not the first time I ask this question, but I have slightly altered the details to hopefully make it clearer. Here is one version of the code I have tried:
Private Sub OpenWbsInPath(zPath As String)
Dim zFile As String
zPath = IIf(Right$(zPath, 1) = "\", zPath, zPath & "\") ' Ensure zPath ends with "\".
zFile = Dir(zPath & "*.xls*") ' "xxxx\*.xls*". OK.
Do While zFile <> ""
Workbooks.Open Filename:=zPath & zFile ' Fails with error 1004 on 2nd iteration.
DoEvents ' Makes no difference.
zFile = Dir() ' Next filename. Path also OK.
Loop
End Sub
I have also tried a similar procedure using CreateObject("Scripting.FileSystemObject"). In all approaches I have tried so far, including one in which I created an array of file names first and then tried to open each file individually, the first file opens OK, then the next file fails to open with error 1004.
The really strange thing is that in an older version of the program, all the files open OK. I have even tried copying the identical code, but it still doesn't work in the new location. I am running out of ideas - do I need to create a temporary folder for each workbook before I open it? Seems crazy.
I've run your code, and can confirm it works fine, if the files can be opened, and fails with error 1004 if they can't.
Possible reasons that a file can't be opened include
it's corrupt, or.
it's already open.
A file can't be open if:
you or your code opened it already (is the file containing the code also in the folder?),
someone else has it open (is the folder shared?),
another instance of Excel has it open (check task manager for extra instances).
Can you manually open the file the code fails on? Add a Debug.Print zFile before the Open to see what file fails

Possibility to use hyperlinks in a shared excel workbook

I've got an excel document where we've enabled the "Track Changes" functionality in order to capture whose changes what and when - for this you'd have to share the workbook.
We've now found that we want to make it a bit easier to navigate in the file (as it's getting quite big) - and in this process we'd like to make use of hyperlinks between tabs.
Now I've looked around a bit to find out how to do this - because as you may know Excel doesn't allow you to use hyperlinks when the workbook is shared (at least not that I know of) - and I don't want to - unshare workbook > insert hyperlink > reshare - as that means losing the tracking we've made so far :(
I've been able to find an answer stating:
In the Formula Bar for the cell you want the link in, use the following formula:
=HYPERLINK("url_goes_here", "cell_content_here")
Now I've tried inserting something like this:
=HYPERLINK("'CHANGE LOG'!"; "CHANGE LOG") and alternatively
=HYPERLINK("'CHANGE LOG'"; "CHANGE LOG")
It does truly insert a hyperlink but I just get an error message stating "cannot open the specified file."
Is anyone aware of a workaround or able to tell if I'm doing something completely wrong?
Br, Thomas
Several other sites suggests to unshare, add links and then reshare the workbook but this is somewhat annoying because it prevents users that currently have it open from properly saving if you unshare while they have it open even if you reshare before they save.
No need to unshare then reshare just set the cell's Formula property to equal to =HYPERLINK(link, text)
Sheets(targetSheet).Range(targetRange).Formula = _
"=HYPERLINK(" & Chr(34) & "http://www.google.ca/" & Chr(34) & ", " _
& Chr(34) & "Click to go to google" & Chr(34) & ")"
Chr(34) are double quotes
The result formula is =HYPERLINK("http://www.google.ca/", "Click to go to google")
This is the workaround in VBA to allow hyperlinks in shared workbooks.
Sorry that this probably doesn't completely answer OP's question but this was the only SO topic regarding this problem so I thought I'd share in case anyone else stumbles here.
Try with this http://office.microsoft.com/en-001/excel-help/hyperlink-function-HP010342583.aspx
Section: Jump to a specific cell on a worksheet

Unintended file dialog when updating links in Excel 2003 VBA

I'm keeping sets of interrelated Excel 2003 spreadsheets for each of my company's projects.
I want to copy some template XLS files to the project name and change the links that connect them to each other.
For example, the file TEMPLATE_ScanProgress.xls links to TEMPLATE_Film_Review.xls.
I am copying them both to 123456_ScanProgress.xls and 123456_Film_Review.xls, and updating the link in 123456_ScanProgress.xls.
Sample code of what I'm doing:
If Dir("WorkOrder & "_ScanProgress.xls") = "" Then
FileCopy "TEMPLATE_ScanProgress.xls", WorkOrder & "_ScanProgress.xls"
Workbooks.Open Filename:=WorkOrder & "_ScanProgress.xls", UpdateLinks:=0
ActiveWorkbook.ChangeLink "TEMPLATE_Film_Review.xls", _
WorkOrder & "_Film_Review.xls", _
xlLinkTypeExcelLinks
Workbooks(WorkOrder & "_ScanProgress.xls").Close SaveChanges:=True
Else
FileExists = True
FileExistsWarning_7 = WorkOrder & "_ScanProgress.xls"
End If
The problem is that when the code tries to update the link I get a file dialog asking me to choose a file for the change, even though I already specified which file I want in the code.
Try setting DisplayAlerts to False. DisplayAlerts is on the Application object and is used to suppress dialog boxes for example when overwriting a file. It may work in this case too.

Deleting hidden name definitions with invalid names in Excel 2003

I am using this macro:
http://support.microsoft.com/kb/119826
to try and clean up hidden names in my excel file. It works for most of the hidden names, but not for a few _123Graph names. I'm not sure where these names came from, but when I try to delete them I get a 1004 automation error.
The knowledge base mentions that names with spaces may cause an error. Is there a way to delete these?
Excel 2007 and above resolves this issue... but a quick fix for Excel 2003 is:
Go to the Tools Menu
- Options
- General tab
- Check R1C1 Reference Style
Then hit "Ok".
You will then be prompted for a new name for each of the corrupted names.
...then you can go back and uncheck the R1C1 check box.
I use the excellent Name Manager add-in to, erm, manage the named ranges in my workbooks, including all those pesky ones like the example you give that are automatically created by Excel when autofiltering etc. which aren't normally exposed.
It allows filtering of names by type, location, scope etc. and generally knocks the awful built-in dialog into next week.
Edit: If installing an add-in is out of the question then adding the following code in a standard module will allow you to loop through the names in the workbook and delete the offending items.
Sub deleteNamedRanges()
Dim n As Name
Dim a As Variant
For Each n In ThisWorkbook.Names
a = MsgBox("Do you want to delete the following name?:" & vbCrLf & vbCrLf & n.Name & " (" & n.RefersTo & ")", vbYesNo, "Delete ranges")
If a = vbYes Then
n.Delete
End If
Next n
End Sub
If there are a great many names then you should be able to modify this to suit your needs.

Resources