I have searched but haven’t found what I am looking for; appreciate any help given.
I need to create a macro in a workbook that will find an Excel workbook that is already open and where cell A1 contains the word ‘Network’ on the 2nde visible sheet. Once it finds that Excel, it needs to run other Subs, which I know how to create. I am stuck on how to find the open Excel workbook with the criteria mentioned.
Function FindOpenExcel(ByRef targetWb As Workbook, ByRef targetWs As Worksheet) As String
Dim wb As Workbook
Dim ws As Worksheet
Dim cell As Range
Dim visibleSheet As Integer
Set targetWb = Nothing
Set targetWs = Nothing
For Each wb In Workbooks
visibleSheetCount = 0
For Each ws In wb.Sheets
If ws.Visible = xlSheetVisible Then
visibleSheetCount = visibleSheetCount + 1
If visibleSheetCount = 2 Then
Exit For
End If
End If
Next ws
If visibleSheetCount < 2 Then
GoTo NextWorkBook
End If
For Each cell In ws.Rows(1).Cells
If InStr(1, cell.Value, "Network", vbTextCompare) > 0 Then
Set targetWb = wb
Set targetWs = ws
FindOpenExcel = cell.Value
Exit Function
End If
Next cell
NextWorkBook:
Next wb
FindOpenExcel = "No roster found. Reminder: Only one roster can be open and cell A1 must contain the word 'Network'."
End Function