VBA - A document with the name '' is already open - vba

I have written a macro in Excel that I assigned to a button in a workbook. When pressed it asks a user for a file, opens that file and copies the contents into the sheet2 in the original workbook, then saves the original workbook under a new name. It then creates a new button on the worksheet to run another subroutine. When pressing the button a window pops up saying "A document with the name (document name) is already open. Is there anyway to resolve this? I assume this is happening because I'm saving the original workbook under a new name.
Sub openFile()
Dim tempWB As Object, btn As Button, desktopPath As String
fileToOpen = Application.GetOpenFilename(Title:="Select transaction history export:")
If fileToOpen <> False Then
Set tempWB = Workbooks.Open(Filename:=fileToOpen)
End If
If fileToOpen = False Then
End
End If
ActiveSheet.Cells.Copy
Workbooks("Test.xlsm").Sheets("Sheet2").Activate
ActiveSheet.Cells.ClearContents
ActiveSheet.Cells(1, 1).Select
ActiveSheet.Paste
tempWB.Close
Set btn = ActiveSheet.Buttons.Add(650, 50, 100, 40)
With btn
.OnAction = "action"
End With
desktopPath = (MacScript("(path to desktop from user domain as string)") & ":")
ActiveWorkbook.SaveAs Filename:=(desktopPath & "Result" & VBA.format(Date, "mm/dd/yy") & ".xlsm")
End Sub
Sub action()
Range("A1:B1").Font.Size = 14
End Sub

Related

Workbook_Open Subscript out of Range

I am having issues with writing a macro in VBA when it is being initiated with the opening of my workbook. The error reads 'Error 9 - Subscript out of range'.
The Macro should look to see if there is a 'control' sheet in the current workbook, if not then it will open another workbook and copy the control sheet over - closing that workbook as it does so.
This is a strange one because it actually works if I attach it to a button, but doesn't work when I try and initiate the Macro when you open the file.
Here is my code;
Private Sub Workbook_Open()
' CreateEUC Macro
ScreenUpdating = False
For i = 1 To Worksheets.Count
If Worksheets(i).Name = "Control" Then
exists = True
MsgBox ("There is already an EUC slide in this workbook")
End If
Next i
If Not exists Then
' Open Location
Workbooks.Open "T:\Pricing\EUC Inventory\EUC Control Sheet v0.4.xlsx"
' Copy/Paste EUC
Sheets("Control").Copy After:=ThisWorkbook.Sheets(1)
' Close EUC Workbook
Workbooks("EUC Control Sheet v0.4.xlsx").Close savechanges:=False
' Move sheet at front of workbook
Sheets("Control").Move Before:=Sheets(1)
Range("A1:H1").Select
End If
ScreenUpdating = True
End Sub
As you haven't qualified which workbook contains worksheet "Control", you're implicitly referring to the activeworkbook, and you've already proven that worksheet "Control" doesn't exist... Qualify your worksheet reference (oh, and always declare your variables!)
Private Sub Workbook_Open()
Dim i As Integer
Dim exists As Boolean
Application.ScreenUpdating = False
For i = 1 To Worksheets.Count
If Worksheets(i).Name = "Control" Then
exists = True
MsgBox ("There is already an EUC slide in this workbook")
End If
Next i
If Not exists Then
With Workbooks.Open("T:\Pricing\EUC Inventory\EUC Control Sheet v0.4.xlsx") ' Open Location
.Sheets("Control").Copy After:=ThisWorkbook.Sheets(1) ' Copy/Paste EUC
.Close savechanges:=False ' Close EUC Workbook
End With
Sheets("Control").Move Before:=Sheets(1) ' Move sheet to front of workbook
Range("A1:H1").Select
End If
Application.ScreenUpdating = True
End Sub

Open save window in file path from a cell well also populateing filename from cell

i have a workbook that i use as a template to make estimates that when i'm done filling out the template there is a macro that creates a new workbook and copies all the sheets of the template workbook to the new one and then removes all the formulas and info i don't want the customer to see.
Here's part of my code that creates the new workbook and copies all the sheets from the template to the new one and then cleans it up
Sub TestConvert()
'Disabling the following to speed up the vba code, must re-enable at end of code
ActiveSheet.DisplayPageBreaks = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'adds file name and path to all sheets
Dim WSfn As Worksheet
For Each WSfn In ThisWorkbook.Sheets
'Adds formula to show file path
WSfn.Range("A2") = "=LEFT(CELL(""filename"",RC),FIND(""["",CELL(""filename"",RC),1)-1)"
'Adds formula to show file name
WSfn.Range("A3") = "=MID(CELL(""filename""),FIND(""["",CELL(""filename""))+1,(FIND(""]"",CELL(""filename""))-FIND(""["",CELL(""Filename""))-16))"
WSfn.Calculate 'Calculate sheet
WSfn.Range("A2") = WSfn.Range("A2") 'this will remove the formula from the cell making it text only
WSfn.Range("A3") = WSfn.Range("A3") 'this will remove the formula from the cell making it text only
Next
'************************************************************************************************
'copies all the sheets of the open workbook to a new one
Dim thisWb As Workbook, wbTemp As Workbook
Dim ws As Worksheet
Set thisWb = ThisWorkbook
Set wbTemp = Workbooks.Add 'creates new workbook dimmed as WbTemp
On Error Resume Next 'if there is in error when deleting will not stop the macro from continuing...
'.. deletes the extra sheets 2 sheets if on an older versions of excel
For Each ws In wbTemp.Worksheets
ws.Delete 'deletes all but one sheet in new workbook
Next
On Error GoTo -1 'clears the error handling and sets it to nothing which allows you to create another error trap.
'copys all the sheets from the original to the new workbook dimmed as wbTemp
For Each ws In thisWb.Sheets
ws.Copy After:=wbTemp.Sheets(wbTemp.Worksheets.Count)
Next
wbTemp.Sheets(1).Delete 'deletes the the first sheet in the list in the new workbook which is a black sheet from creating a new workbook
'put vba code to be ran in new book here
'makes all formulas in new workbook values only
wbTemp.Sheets.Select 'selects all sheets in new workbook
Cells.Select 'selects all cell
Selection.Copy 'copies everything selected
Selection.PasteSpecial Paste:=xlPasteValues 'pastes as values only in selected cells
wbTemp.Application.CutCopyMode = False 'clears the clipbored
'removes all defind names from new workbook / submittal
Dim xName As Name
For Each xName In wbTemp.Names
xName.Delete
Next
'removes all dropdowns from new workbook / submittal
Dim DD As Worksheet
For Each DD In wbTemp.Worksheets
Cells.Select
DD.Cells.Validation.Delete
Range("A1").Select
Next
'removes all vba buttons from all sheets
Dim i As Integer
On Error Resume Next
For i = 1 To 1000
wbTemp.Sheets(i).Buttons.Delete
Next i
'All sheets scroll to top left and select "A1"
Dim Sht As Worksheet
'****************************
'change A1 to suit your preference
Const TopLeft As String = "A1"
'****************************
'loop thru all the sheets in the workbook
For Each Sht In Worksheets
'scroll:=True takes cell to the top-left of window
Application.Goto Sheet.Range(TopLeft), scroll:=True
Next
'Hides the following from all sheets
wbTemp.Sheets.Select 'selects all sheets in new workbook
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayHeadings = False
'selects the first sheet in the list
Sheets(1).Select
ActiveSheet.DisplayPageBreaks = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayAlerts = True
'save vba code here
'works to only add the filename would like it to also open in file path from cell A2
Application.Dialogs(xlDialogSaveAs).Show Range("A3").Text & "- (Submittal) " & Format(Date, "mm-dd-yy") & "_" & Format(Time, "hhmm") & ".xlsx"
End Sub
im wanting to make it so when the save window opens it opens in the file path from cell A2 and populates the filename from cell A3
i can also send/post the full excel file if that helps any.
The Application.GetSaveAsFilename method is a good choice for this. Pass the return value to a variant type var so you can test for Cancel or Close.
Dim sFN As Variant
With Worksheets("Sheet6")
sFN = .Range("A1") & Chr(92) & .Range("A2") & Format(Date, "_mm-dd-yy") '<~~ no extension yet
End With
With Application
sFN = .GetSaveAsFilename(InitialFileName:=sFN, _
FileFilter:="Excel Workbook (*.xlsx), *.xlsx," & _
"Macro Workbook (*.xlsm), *.xlsm," & _
"Binary Workbook (*.xlsb), *.xlsb")
End With
Select Case sFN
Case False
'user clicked Cancel or Close (×)
Debug.Print sFN
Case Else
With ThisWorkbook
Select Case Right(sFN, 5)
Case ".xlsx"
.SaveAs Filename:=sFN, FileFormat:=xlOpenXMLWorkbook
Case ".xlsm"
.SaveAs Filename:=sFN, FileFormat:=xlOpenXMLWorkbookMacroEnabled
Case ".xlsb"
.SaveAs Filename:=sFN, FileFormat:=xlExcel12
Case Else
'there really shouldn't be a case else
End Select
End With
End Select
I've added a Select Case statement statement for a Workbook.SaveAs method to three msot common types of Excel workbooks.
You can use the .InitialFileName property of the dialog.
Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1")
Dim oFileDialog As FileDialog
Set oFileDialog = Application.FileDialog(msoFileDialogSaveAs)
With oFileDialog
.Title = "Save File"
.ButtonName = "Ok"
.InitialFileName = ws.Range("A2").Value & "\" & ws.Range("A3").Value
.Show
End With
If you need to get back the name that it was saved as you can use .SelectedItems after .Show
MsgBox (oFileDialog.SelectedItems(1))
NOTE:
You probably want to do a quick verification that the directory in A2 exists before doing this. If it does not exist it will throw this into some users folder.
EDIT I'm not sure why yours isn't saving, could be excel version or some other variable in your code.
Since you have the path and name, do you really need the saveas dialog? You could just do
Workbooks.Add
'Then your code in your template that is modifying the active workbook
'Then save it without the dialog
ActiveWorkbook.SaveAs ws.Range("A2").Value & "\" & ws.Range("A3").Value
'OR
ActiveWorkbook.SaveAs Filename:= ws.Range("A2").Value & "\" & ws.Range("A3").Value

Programmatically create Excel workbooks which will display a user form on opening

In the automation tool trainer has to mention student names and using that name the Excel file will be created. Example: Shreesha_xlsx.xlsx
After assigning the test with their names, IF the students open the Excel file of their own, THEN they should be able to see the userform (Welcome screen) and next screen is answering the questions.
The following code is that to assign the Excel file under the student name and after that I have also pasted the code that when user clicks on the Excel file it should open with userform.
Altogether it is calling userform of one Excel file in another without setting any reference.
Sub Button2_Click()
Dim s(6 To 100) As String
Dim stname As String
Dim status As String
Dim mypath As String
Dim u As String
u = "_xlsx"
For i = 6 To 100
s(i) = Range("E" & i).Value
stname = s(i) & "" & u
If s(i) = "" Then
ActiveWorkbook.Open = False
End If
'in case of Run time error
On Error GoTo jamun:
mypath = Range("B1").Value & "\" & stname
Workbooks.Add.SaveAs filename:=mypath
ActiveWorkbook.Close
Range("B" & i).Value = mypath & "_assigning..."
Application.Wait Now + TimeValue("00:00:02")
Range("F" & i).Value = "Done"
Range("B" & i).Value = mypath & "_assigned"
Range("B" & i).Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="mypath", TextToDisplay:=Range("B" & i).Value
Range("B" & i).Select
Selection.Hyperlinks(1).Address = Range("B1").Value
Application.Wait Now + TimeValue("00:00:01")
Next
MsgBox "Test assigned successfully"
Exit Sub
jamun:
MsgBox "Test assigned successfully"
End Sub
Now the following code is that when they open, they should see the userform
enter code here
Sub Workbook_Open()
Dim FSO As New FileSystemObject
Dim objFolder As Folder
Dim objFile As File
Dim strpath As String
Dim a As Workbook
Dim filename As String
strpath = Range("B1").Value
Set objFolder = FSO.GetFolder(strpath)
If objFolder.Files.Count = 0 Then
MsgBox "No files were found....", vbExclamation
End If
For Each objFile In objFolder.Files
a = "Good" 'userform name is good
Workbook.Open (a)
VBA.UserForms.Add(a).Show
a.Show
Next objFile
End Sub
and don't know where I am going wrong.
1) Create an excel workbook with the desired form
2) Write code to open the form on Workbook_Open()
3) Write code in the form to fill the workbook with questions and whatever other information when the WB opens. All in all, make the behavior of the file exactly as you want it to behave when it opens to the student.
4) Save your file as a template (extension .xltm), let's say examTemplate.xltm
5) Now when you will generate exam files from the master file, generate them from the template. Consider changing this part of your code:
Workbooks.Add.SaveAs filename:=mypath
ActiveWorkbook.Close
Instead of this, we generate the file from the template:
Dim neWB as Workbook
Set newWB = Workbooks.Add("examTemplate.xltm") ' <~~~ generate from template
The freshly generated newWB inherits the template. That is, it has all its data, code, controls and forms. At this point, you can fill some data in newWB, things related to the assignment. That is, questions, or some parameters that will indicate where to fetch the questions, so that the form can access these parameters and do the work. Ideally, these parameters can be embedded in a hidden sheet. After then:
newWB.SaveAs filename:=mypath ' <~~ save it as macro-enabled .xlsm
newWB.Close
From that point, I think you can continue with with the same logic. The radical change in the approach is that the form will be readily embedded in the new workbook, not invoked from another workbook.
EDIT: you want your workbook to only show only the Form but never the workbook itself. This can be achieved by adding the following event handler to the ThisWorkbook code module of your template file:
Private Sub Workbook_Open()
If InStr(1, Me.Name, ".xltm") > 1 Then Exit Sub ' <~~ to apply only to chidren no to template itself
With Me.Application
.Visible = False
.DisplayAlerts = False
MyForm.Show
.Visible = True
End With
Me.Close
End Sub

Excel VBA export to other excel workbooks

I dont have plenty of experience in VBA excel so i could use some help.
I created an excel worksheet.
Now i want to create a macro which sends the data from my worksheet to other excel workbooks.
I want to use an if statement so if project name = "x" then the macro should send data to workbook "x" and rank the imported worksheets by date.
I found this on the web and had adjusted it a bit
Private Sub CommandButton21_Click()
Dim Data As Range
Dim myData As Workbook
Worksheets("blad1").Select
Set Data = Range("c2")
Set myData = Workbooks.Open("C:\test\locatie.xlsx")
Worksheets("blad1").Select
Worksheets("blad1").Range("a1").Select
RowCount = Worksheets("blad1").Range("A1").CurrentRegion.Rows.Count
With Worksheets("blad1").Range("A1")
.Offset(RowCount, 0) = Data
End With
End Sub
Simple example to copy a sheet into a new file:
IF projectname = "x" THEN 'you have to define projectname
OldName = ThisWorkbook.Name 'name of your open file
Workbook.Add 'Open new file
newName = ActiveWorkbook.Name 'name of the new file
Windows(OldName).Activate 'original file select
Sheets("Sheetname").Activate 'define Sheetname of your original file
ActiveSheet.Select
ActiveSheet.Copy after:=Workbooks(newName).Sheets(1)
Windows(newName).Activate
ActiveWorkbook.SaveAs Filename:=filename1, FileFormat:=51, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False 'define filename1
Application.DisplayAlerts = True
End If

Macro that runs a Macro that opens files and save them as value - Runtime Error 1004

I keep getting this 1004 runtime error. I have slimmed my programing down some so it’s not so Programception. I think it may have to do with using Excel 2010 to save .xls files. Not sure.
When Auto_Root.xls opens it runs Sub auto_open() which opens
Panel.xls
Panel opens and runs Sub Update() which sequentially opens 7 files
in different directories all called Auto_Update.xls
Auto_Update.xsl opens and runs Sub Flat which each open a number of
files sequentially and saves a flat copy of themselves in another
directory.
I have opened each of the 7 Auto_Update.xls files and have run them independently and they run with no errors. When I run them all from Auto_Root I get a runtime error 1004. And CurrentWB.Save is highlighted on one of the files. I even replaced CurrentWB.Save as CurrentWB.SaveAs Filename:=TargetFile, FileFormat:=xlNormal and recieved the same runtime error.
Attached is the code I have.
AutoRoot.xls!Auto Update
Sub auto_open()
Application.CutCopyMode = False
Dim PanelFilePath As String
Dim PanelFileName As String
Dim PanelLocation As String
Dim PanelWB As Workbook
PanelFilePath = "D:\umc\UMC Production Files\Automation Files\"
PanelFileName = "Panel.xls"
PanelLocation = PanelFilePath & Dir$(PanelFilePath & PanelFileName)
Set PanelWB = Workbooks.Open(Filename:=PanelLocation, UpdateLinks:=3)
PanelWB.RunAutoMacros Which:=xlAutoOpen
Application.Run "Panel.xls!Update"
PanelWB.Close
Call Shell("D:\umc\UMC Production Files\Automation Files\Auto.bat", vbNormalFocus)
Application.Quit
End Sub
Panel.xls!Update
Sub Update()
Dim RowNumber As Long
Dim AutoUpdateTargetFile As String
Dim AutoUpdateWB As Workbook
For RowNumber = 1 To (Range("AutoUpdate.File").Rows.Count - 1)
If (Range("AutoUpdate.File").Rows(RowNumber) <> "") Then
AutoUpdateTargetFile = Range("Sys.Path") & Range("Client.Path").Rows(RowNumber) & Range("AutoUpdate.Path ").Rows(RowNumber) & Range("AutoUpdate.File").Rows(RowNumber)
Set AutoUpdateWB = Workbooks.Open(Filename:=AutoUpdateTargetFile, UpdateLinks:=3)
AutoUpdateWB.RunAutoMacros Which:=xlAutoOpen
Application.Run "Auto_Update.xls!Flat"
AutoUpdateWB.Close
End If
Next RowNumber
End Sub
AutoUpdate.xls!Flat
Sub Flat()
Dim RowNumber As Long 'Long Stores Variable
Dim SheetNumber As Long
Dim TargetFile As String 'String Stores File Path
Dim BackupFile As String
Dim CurrentWB As Workbook 'Workbook Stores Workbook
For RowNumber = 1 To (Range("File").Rows.Count - 1)
'Loops through each file in the list and assigns a workbook variable.
If (Range("File").Rows(RowNumber) <> "") Then
TargetFile = Range("Sys.Path") & Range("Path").Rows(RowNumber) & Range("File").Rows(RowNumber) 'Target File Path
BackupFile = Range("Report.Path") & Range("Path").Rows(RowNumber) & Range("SubFolder") & Range("File").Rows(RowNumber) 'Backup File Path
Set CurrentWB = Workbooks.Open(Filename:=TargetFile, UpdateLinks:=3) 'Sets CurrentWB = to that long name. This becomes the name of the workbook.
CurrentWB.RunAutoMacros Which:=xlAutoOpen 'Enables Macros in Workbook
CurrentWB.SaveAs Filename:=TargetFile, FileFormat:=56
For SheetNumber = 1 To Sheets.Count 'Counts Worksheets in Workbook
Sheets(SheetNumber).Select 'Selects All Worksheets in Workbook
If (Sheets(SheetNumber).Name <> "What If") Then
Sheets(SheetNumber).Unprotect ("UMC626") 'Unprotects Workbook
Cells.Select 'Selects Data in Workbook
Range("B2").Activate
With Sheets(SheetNumber).UsedRange
.Value = .Value
End With
Sheets(SheetNumber).Protect Password:="UMC626", DrawingObjects:=True, Contents:=True, Scenarios:=True 'Protects Workbook
End If
Next SheetNumber 'Runs Through Iteration
Sheets(1).Select
Range("A1").Select 'Saves each workbook at the top of the page
CurrentWB.SaveAs Filename:=BackupFile, FileFormat:=56, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False 'Saves Workbook in Flatten File Location
CurrentWB.Close 'Closes Workbook
End If 'Ends Loop
Next RowNumber 'Selects Another Account
End Sub
What I have done so far.
Each Individual AutoUpdate file works when ran on its on.
If Application.Run"Auto_Update.xls!Flat" is removed from Panel.xls!Update it opens and closes all of the AutoUpdate.xls files with no error.
If I link Panel.xls!Update to only 3 of the 7 AutoUpdate files.... any 3. It runs with no errors.
I just can't seem to get it to run all 7 without saying Runtime Error 1004.
I found a microsoft work around code. Not sure how to implement it though.
Sub CopySheetTest()
Dim iTemp As Integer
Dim oBook As Workbook
Dim iCounter As Integer
' Create a new blank workbook:
iTemp = Application.SheetsInNewWorkbook
Application.SheetsInNewWorkbook = 1
Set oBook = Application.Workbooks.Add
Application.SheetsInNewWorkbook = iTemp
' Add a defined name to the workbook
' that RefersTo a range:
oBook.Names.Add Name:="tempRange", _
RefersTo:="=Sheet1!$A$1"
' Save the workbook:
oBook.SaveAs "c:\test2.xls"
' Copy the sheet in a loop. Eventually,
' you get error 1004: Copy Method of
' Worksheet class failed.
For iCounter = 1 To 275
oBook.Worksheets(1).Copy After:=oBook.Worksheets(1)
'Uncomment this code for the workaround:
'Save, close, and reopen after every 100 iterations:
If iCounter Mod 100 = 0 Then
oBook.Close SaveChanges:=True
Set oBook = Nothing
Set oBook = Application.Workbooks.Open("c:\test2.xls")
End If
Next
End Sub
http://support.microsoft.com/kb/210684/en-us
Based on the document from Microsoft linked below this is a known issue.
Copying worksheet programmatically causes run-time error 1004 in Excel
I'm not sure how many sheets this loop in Flat but it appears that is the issue. Specifically the quote:
This problem can occur when you give the workbook a defined name and then copy the worksheet several times without first saving and closing the workbook
Due to the levels that you have created using separate workbooks I would suggest starting with limiting the scope of your Update subroutine. There are many designs for something like that but I might start with passing an integer argument back and fourth between Auto Open and Update. That way you can close and reopen Panel.xls multiple times and start exactly where you left off.
Its not clear from your text, but is your procedure "Flat" inside the files you are opening and if so is it being called by the auto open macro?
It sounds like you want to only be running your macro from your original workbook, and not firing the ones in the auto open macro of the workbooks you open.
If this is indeed the case, I do something similar in one of my workbooks, where I have an "upgrade" wizard that fires when the work book is opened, however because I am upgrading, the other workbook I open, also has the upgrade wizard, and so that used to fire as well. I resolved this by opening the other workbook in a hidden instance of excel, and within my auto open macro, I have a line of code that queries the visible state of the workbook, and does not fire if it is hidden. So in the below code its the "And Me.Application.visible" that controls if the wizard is run
'Check if the ODS code is populated or default xxx, if so invoke the upgrade wizard
'but only if the application is visible
If (ActiveWorkbook.Names("Trust_ODS_Code").RefersToRange.Value = "xxx" _
Or Len(ActiveWorkbook.Names("Trust_ODS_Code").RefersToRange.Value) = 0) _
And Me.Application.visible = True Then
'run the upgrade wizard
frmCSCWizardv8.Show
End If
This requires that you open your workbooks in a separate excel instance. The below code is the snippet of code that does this, hope this is enopugh for you to get the idea
Dim lRet
Dim i As Integer, j As Integer
Dim FoundSheet As Boolean
'Because the wizard opens the old DCS in a hidden instance of Excel, it is vital that we close this if
'anything goes wrong, so belt and braces, close it every time the user presses the button
'Switch off the error handling and the display alerts to avoid any error messages if the old dcs has
'never been opened and the hidden instance does not exist
Application.DisplayAlerts = False
On Error Resume Next
book.Close SaveChanges:=False
app.Quit
Set app = Nothing
Application.DisplayAlerts = True
'set error handling
On Error GoTo Err_Clr
'populate the status bar
Application.StatusBar = "Attempting to open File"
'Default method Uses Excel Open Dialog To Show the Files
lRet = Application.GetOpenFilename("Excel files (*.xls;*.xlsx;*.xlsm;*.xlsb), *.xls;*.xlsx;*.xlsm;*.xlsb")
'If the user selects cancel update the status to tell them
If lRet = False Then
Me.lstOpenDCSStatus.AddItem "No file selected"
'if the user has selected a file try to open it
Else
'This next section of code creates a new instance of excel to open the selected file with, as this allows us to
'open it in the background
OldDCS = lRet
Application.StatusBar = "Attempting to open File - " & lRet
app.visible = False 'Visible is False by default, so this isn't necessary, but makes readability better
Set book = app.Workbooks.Add(lRet)
Application.StatusBar = "Opened File - " & lRet