I have 2 spreadsheets, Test123.xlsx and Test456.xlsx. The VBA code I have written opens Test456.xlsx, and writes some information to it. I also want to activate Test123.xlsx which will already be open when I run the code and write some information to that but I keep getting a Subscript out of Range error message. Can you please assist me with this? My code is as follows:
VBA Code:
Sub RiskSummaryStatsHideMovt()
' RiskSummaryStatsHideMovt Macro
'
' Open Test456 if not already open
Ret = IsWorkBookOpen("H:\_Misc\_Misc\Test456.xlsx")
If Ret = False Then
Set wbLineage1 = Workbooks.Open("H:\_Misc\_Misc\Test456.xlsx")
wbTest456 = "Test456.xlsx"
End If
strFilePath = "H:\_Misc\_Misc\"
strFileName = "Test123.xlsx"
strsOpenSpreadsheet = strFilePath & strFileName
Set OpenSpreadsheet = Workbooks(strFileName) 'Error Message appears on this line of code
Workbooks(wbTest456).Activate
Worksheets("Sheet456").Range("A2").Select
ActiveCell.Value = "Hello World2"
End Sub
Function IsWorkBookOpen(FileName As String)
Dim ff As Long, ErrNo As Long
On Error Resume Next
ff = FreeFile()
Open FileName For Input Lock Read As #ff
Close ff
ErrNo = Err
On Error GoTo 0
Select Case ErrNo
Case 0: IsWorkBookOpen = False
Case 70: IsWorkBookOpen = True
Case Else: Error ErrNo
End Select
End Function