Macro Help, selecting inactive workbook

JoshuaGA

New Member
Joined
May 4, 2009
Messages
15
Hi I need help completing this Macro. I have the first part below that will open a specified workbook based off a dynamic reference in cell "P9" on my Mapping tab in order top copy some content and paste into the Macro Workbook...

What I need help doing next is activating, or selecting the workbook that was opened through strFName so I can close it and move on to opening the next workbook. Or is there a way to just close the inactive workbook without saving it? Thanks!

Sub Test()
Dim strFName As String

strFName = ThisWorkbook.Worksheets("Mapping").Range("P9").Value
'this variable contains the workbook name and path
If FileExists(strFName) Then
'does it exist?
If Not BookOpen(Dir(strFName)) Then Workbooks.Open Filename:=strFName
'if its not already open, open it
Else
MsgBox "The file does not exist!"
End If

ActiveWorkbook.Unprotect
Application.ScreenUpdating = False 'speed up macro
Application.DisplayAlerts = False

Sheets("Spot Report").Visible = True
Sheets("Spot Report").Select
Range("C2:O2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

ThisWorkbook.Activate

Sheets("Current Yr").Select
ActiveSheet.Cells(2, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues

End Sub
Function FileExists(strfullname As String) As Boolean
FileExists = Dir(strfullname) <> ""
End Function
Function BookOpen(strWBName As String) As Boolean
Dim wbk As Workbook
On Error Resume Next
Set wbk = Workbooks(strWBName)
If Not wbk Is Nothing Then BookOpen = True
End Function
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You can store a reference of the workbook when you open it. Later use that reference to activate/close/manipulate that workbook.


Code:
If Not BookOpen(Dir(strFName)) Then [COLOR=#0000ff]Set oWB = [/COLOR]Workbooks.Open(Filename:=strFName)
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,757
Members
449,094
Latest member
dsharae57

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