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
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