VBA - Close Workbook with varying name

julia55

Board Regular
Joined
Dec 2, 2010
Messages
73
I am basically making a template that multiple people will use. I need the macro to open a workbook, and then later close the workbook it opened after running a few steps. The opened workbook may not always have the same name. I would really like to get away with not using specific workbook names at all, not even for the active workbook. The code is below and I have put in bold the parts I'm haivng issue with.

Code:
Sub PullInResults()
Application.ScreenUpdating = False
Application.Calculation = xlManual
    Sheet2.Select
    Cells.Select
    Selection.ClearContents
    Range("A1").Select
[B]DAWRKBK = ThisWorkbook.Name
Set DstWks1 = ThisWorkbook.Worksheets("Query Results")[/B]
[B]NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.csv), *.csv", Title:="Please select the query results file you just saved")
    If NewFN = False Then
    'They pressed Cancel
MsgBox "Stopping because you did not select a file"
Exit Sub
Else
Workbooks.Open Filename:=NewFN
[/B]End If
Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
Workbooks(DAWRKBK).Activate
Sheet2.Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.CutCopyMode = False
 
[B]Windows("FCO Query Results.csv").Close
Windows("FCO_Summary.xlsm").Activate[/B]
 
Sheet3.Select

End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I even tried this, but can't get it to work, I get an error that it is expecting an End Sub before the Function section.

Code:
Sub tst2()
Dim sFileName As String
sFileName = Application.GetOpenFilename(FileFilter:="Excel Files (*.csv), *.csv", Title:="Please select the query results file you just saved")
If sFileName = "False" Then Exit Sub
Workbooks.Open fileName:=sFileName
Dim temp As String
temp = spliceFileNameEnd2(sFileName)
MsgBox (temp)
Workbooks(temp).Close
Function spliceFileNameEnd2(fileName As String) As String
Dim x As Integer
Dim stringLength As Integer
Dim lastSlash As Integer
Dim temp As String
stringLength = Len(fileName)
For x = 1 To stringLength
    temp = Mid(fileName, x, 1)
    If temp = "\" Then
        lastSlash = x
    End If
Next x
 
spliceFileNameEnd2 = Mid(fileName, lastSlash + 1, stringLength)
 
End Function
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top