Using ISREF with closed workbooks

YasserKhalil
PlatinumLounger
Posts: 4936
Joined: 31 Aug 2016, 09:02

Using ISREF with closed workbooks

Post by YasserKhalil »

Hello everyone
I am trying to loop through closed workbooks to copy all the worksheets within the closed workbooks
I need to add a condition if the worksheet name in ThisWorkbook , then to skip that worksheet and not to copy it
I have used this line

Code: Select all

If Not Evaluate("ISREF('" & ws.Name & "'!A1)") Then
To make sure the worksheet is not exist and that doesn't work
I have reversed the condition by removing Not but the same problem

This is also another try but I got an error

Code: Select all

If Not Evaluate("ISREF('[" & ThisWorkbook.Name & "]" & ws.Name & "'!A1)") Then
I think this is because the ISREF may not deal with the closed workbooks or how can I make it deal with ThisWorkbook?

YasserKhalil
PlatinumLounger
Posts: 4936
Joined: 31 Aug 2016, 09:02

Re: Using ISREF with closed workbooks

Post by YasserKhalil »

I have tried this line and this works for me

Code: Select all

If IsError(Evaluate("ISREF('[" & ThisWorkbook.Name & "]" & ws.Name & "'!A1)")) Then
But I welcome any correction or any suggestions

User avatar
HansV
Administrator
Posts: 78890
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using ISREF with closed workbooks

Post by HansV »

Another option would be

Code: Select all

    Dim wsh As Worksheet
    Set wsh = Nothing
    On Error Resume Next
    Set wsh = ThisWorkbook.Worksheets(ws.Name)
    On Error GoTo 0
    If wsh Is Nothing Then
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4936
Joined: 31 Aug 2016, 09:02

Re: Using ISREF with closed workbooks

Post by YasserKhalil »

Thanks a lot Mr. Hans
Your solution is perfect ...
My issue is with ISREF ( I have a UDF that I can use and do the same) but I am asking for how to deal with ISREF with ThisWorkbook as a reference
Regards

User avatar
HansV
Administrator
Posts: 78890
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using ISREF with closed workbooks

Post by HansV »

What was the problem you had with the line

Code: Select all

If Not Evaluate("ISREF('" & ws.Name & "'!A1)") Then
It works correctly for me...
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4936
Joined: 31 Aug 2016, 09:02

Re: Using ISREF with closed workbooks

Post by YasserKhalil »

Yes you are right Mr. Hans
This works for me too but if there are no other workbooks open
My code is looping through the closed workbooks and then loop through each worksheet within those closed workbooks (the target is to copy the worksheets to ThisWorkbook)
And at the same time if the worksheet that will be copied from the other workook is existing in ThisWorkbook then to skip copying that worksheet ...

YasserKhalil
PlatinumLounger
Posts: 4936
Joined: 31 Aug 2016, 09:02

Re: Using ISREF with closed workbooks

Post by YasserKhalil »

This is the whole code

Code: Select all

Sub Test()
    Dim wb          As Workbook
    Dim ws          As Worksheet
    Dim myPath      As String
    Dim myFile      As String

    myPath = ThisWorkbook.Path & "\"
    myFile = Dir(myPath & "*.xls*")

    Application.ScreenUpdating = False
        Do While myFile <> ""
            If UCase(myFile) <> UCase(ThisWorkbook.Name) Then
                Set wb = Workbooks.Open(myPath & myFile, False)
    
                For Each ws In wb.Worksheets
                    If IsError(Evaluate("ISREF('[" & ThisWorkbook.Name & "]" & ws.Name & "'!A1)")) Then
                        ws.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
                    End If
                Next ws
                wb.Close SaveChanges:=True
            End If
            myFile = Dir
        Loop
    Application.ScreenUpdating = True
End Sub
It is working now using IsError ...

User avatar
HansV
Administrator
Posts: 78890
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using ISREF with closed workbooks

Post by HansV »

I replaced the line

Code: Select all

                    If IsError(Evaluate("ISREF('[" & ThisWorkbook.Name & "]" & ws.Name & "'!A1)")) Then
in your macro with

Code: Select all

                    If Not Evaluate("ISREF('" & ws.Name & "'!A1)") Then
and it worked as intended, without error messages. So I repeat the question that you didn't answer: what was the problem you had with that line? Just saying "doesn't work" doesn't help.
Best wishes,
Hans

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: Using ISREF with closed workbooks

Post by Doc.AElstein »

Hi Yasser,
Like Hans, I am also struggling to understand your current problem ??

But some things might help…
See demo code below

Rem 1
If I have a worksheet with the name “Sheet1” in a workbook that is open, then I get True in all Chk if the coding is in that open workbook

Rem 2 ....Using ISREF with closed workbooks ......????????
I have not found any documentation to confirm yet, but it appears to me that the Excel ISREF function does not work on a closed workbook. I am not sure about this as I can’t find any documentation to confirm that yet. I might be wrong here..

Rem 3
There is a way to get the worksheet names from a closed workbook: In the last code section below, Rem 3 , I was able to get the first worksheet name from the File "ClosedWorkbook.xlsm" . File "ClosedWorkbook.xlsm" was closed and was in the same Folder as the File in which I ran the routine from
( I do not understand how Rem 3 section works. It has worked for me in many different situations. But if it does not work for you then I can’t help further on that).

Code: Select all

Sub TestRef() ' http://www.eileenslounge.com/viewtopic.php?f=30&t=31883&p=247018#p247018
Rem 1
Dim Chk As Boolean
Let Chk = Evaluate("ISREF('" & "Sheet1" & "'!G6)")
Let Chk = Evaluate("ISREF('[" & ThisWorkbook.Name & "]" & "Sheet1" & "'!W12)")
Let Chk = Evaluate("ISREF('" & ThisWorkbook.Path & "\[" & ThisWorkbook.Name & "]" & "Sheet1" & "'!S2)")

Rem 2 This does not seem to work, even if the path and names are valid
Dim CkkClsd As Boolean
Let CkkClsd = Evaluate("ISREF('" & ThisWorkbook.Path & "\[" & "ClosedWorkbook.xlsm" & "]" & "Sheet1" & "'!S2)")

Rem 3 get worksheet name from a closed workbook
 On Error GoTo LEnd
Dim ShtName As String, ShtIndex As Long: Let ShtIndex = 1 ' change number to get sheet name
Dim oRST As Object: Set oRST = CreateObject("ADODB.Recordset"): Dim oConn As Object
Dim sConnString As String: Let sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\" & "ClosedWorkbook.xlsm" & ";Extended Properties=""Excel 12.0 Xml;HDR=No;IMEX=1"";" 'Should work for .xls .xlsx and .xlsm Files
 Set oConn = CreateObject("ADODB.Connection"): oConn.Open sConnString: Set oRST = oConn.OpenSchema(20) '20 =adSchemaTables
 oRST.MoveFirst
    Do Until oRST.EOF: Dim Cnt As Long: Let Cnt = Cnt + 1  'Loop through Table names
        If Right(Replace(oRST("TABLE_NAME"), "'", ""), 1) = "$" Then 'All worksheet names end in $ (after the single quotes denoting a field have been removed)
         Let ShtName = Left(oRST("TABLE_NAME"), Len(oRST("TABLE_NAME")) - 1) 'Exclude $ at the end of the tab name
            If Cnt = ShtIndex Then Exit Do ' Leave with current Worksheet name if Count is at Index Number
        End If
         oRST.MoveNext
    Loop
MsgBox Prompt:="Worksheets(" & ShtIndex & ") Name is  " & ShtName
LEnd:
Set oRST = Nothing: Set oConn = Nothing
End Sub
Alan



Ref
https://www.excelforum.com/tips-and-tut ... -item.html" onclick="window.open(this.href);return false;
Last edited by Doc.AElstein on 22 Feb 2019, 11:46, edited 4 times in total.
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

YasserKhalil
PlatinumLounger
Posts: 4936
Joined: 31 Aug 2016, 09:02

Re: Using ISREF with closed workbooks

Post by YasserKhalil »

The line in this doesn't copy any sheets at all
Please have a look and try the file named "MAIN" >> it is working using ISERROR and if you rerun the code existing in the file now, no sheets will be copied again
You do not have the required permissions to view the files attached to this post.

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: Using ISREF with closed workbooks

Post by Doc.AElstein »

Hi yasser,
I do not fully understand how ISREF works yet.
It appears that it will only take a worksheet reference in order to return False. ( It appears to be happy to return a True using a worksheet reference and fuller references, provided the worksheet exists )

You will have to think of another way to check for the existence of a worksheet Name from the second file in the main file.

For example

Code: Select all

Sub Test()
    Dim wb          As Workbook
    Dim ws          As Worksheet
    Dim myPath      As String
    Dim myFile      As String
Dim strLisWbNms As String: Let strLisWbNms = " "
    For Each ws In ThisWorkbook.Worksheets
     Let strLisWbNms = strLisWbNms & ws.Name & " "
    Next ws
    myPath = ThisWorkbook.Path & "\"
    myFile = Dir(myPath & "*.xls*")
        Do While myFile <> ""
            If UCase(myFile) <> UCase(ThisWorkbook.Name) Then
                Set wb = Workbooks.Open(myPath & myFile, False)

                For Each ws In wb.Worksheets
                    If InStr(1, strLisWbNms, " " & ws.Name & " ", vbBinaryCompare) = 0 Then
                        ws.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
                    End If
                Next ws
                wb.Close SaveChanges:=True
            End If
            myFile = Dir
        Loop
End Sub
You may want to add the name to the string of names after you add that worksheet ' ********

Code: Select all

                    If InStr(1, strLisWbNms, " " & ws.Name & " ", vbBinaryCompare) = 0 Then
                        ws.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
                        Let strLisWbNms = strLisWbNms & ws.Name & " " '  *******
                    End If
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: Using ISREF with closed workbooks

Post by Doc.AElstein »

YasserKhalil wrote:issue is with ISREF ......I am asking for how to deal with ISREF with ThisWorkbook as a reference
Application Evaluate works on the Active Workbook , ( I think... )
So this might work

Code: Select all

Sub Test2()
    Dim wb          As Workbook
    Dim ws          As Worksheet
    Dim myPath      As String
    Dim myFile      As String


    myPath = ThisWorkbook.Path & "\"
    myFile = Dir(myPath & "*.xls*")
        Do While myFile <> ""
            If UCase(myFile) <> UCase(ThisWorkbook.Name) Then
                Set wb = Workbooks.Open(myPath & myFile, False)

                For Each ws In wb.Worksheets
                ThisWorkbook.Activate
                    If Application.Evaluate("ISREF('" & ws.Name & "'!Hi42)") = False Then
                        ws.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
                    End If
                Next ws
                wb.Close SaveChanges:=True
            End If
            myFile = Dir
        Loop
End Sub
( you might want to put ThisWorkbook.Activate somewhere else )
Last edited by Doc.AElstein on 22 Feb 2019, 13:12, edited 1 time in total.
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

User avatar
HansV
Administrator
Posts: 78890
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using ISREF with closed workbooks

Post by HansV »

That looks like the solution. (I can't test it at the moment)
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4936
Joined: 31 Aug 2016, 09:02

Re: Using ISREF with closed workbooks

Post by YasserKhalil »

Thanks a lot. That works well
So it seems that the workbook should be activated so as to make ISREF works well...
Regards

User avatar
HansV
Administrator
Posts: 78890
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using ISREF with closed workbooks

Post by HansV »

Evaluate evaluates the formula against the active workbook and worksheet.
After opening another workbook, that workbook becomes the active workbook.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4936
Joined: 31 Aug 2016, 09:02

Re: Using ISREF with closed workbooks

Post by YasserKhalil »

But that means we can't use ISREF when the workbook is inactive as in this example??

User avatar
HansV
Administrator
Posts: 78890
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using ISREF with closed workbooks

Post by HansV »

The problem is with Evaluate, not with ISREF. How would Evaluate know that you want to evaluate a formula against a workbook that is not active? Alan posted the solution for that.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4936
Joined: 31 Aug 2016, 09:02

Re: Using ISREF with closed workbooks

Post by YasserKhalil »

Oh that's clear now. Thanks a lot for great help

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: Using ISREF with closed workbooks

Post by Doc.AElstein »

I can’t seem to find any decent documentation on this stuff, so I usually have to guess and experiment….
Application Evaluate seems to work on the active workbook
Worksheets(“fhkafg”) Evaluate seems to works on the worksheet, in this case with name “fhkafg”
At the same time, you can reference any other worksheet using the ISREF in a worksheet.

Try this… put the test routine , Sub TestWsEvaluate(), in any code module in a workbook.
Activate any other workbook.
Run Sub TestWsEvaluate(),
It seems to work…

Code: Select all

Sub TestWsEvaluate() ' https://www.eileenslounge.com/viewtopic.php?f=30&t=31883&p=247031#p247035
Dim WsInLisWb As Worksheet
    For Each WsInLisWb In ThisWorkbook.Worksheets
    Dim IsIt As Boolean
     Let IsIt = WsInLisWb.Evaluate("ISREF('" & "Sht_1" & "'!G6)"): Debug.Print IsIt
    Next WsInLisWb
End Sub
So you could do what you want to do using Worksheets(“___”) Evaluate
You can use any worksheet in the workbook where the routine is. The workbook with the code in does not need to be active when using Worksheets(“___”) Evaluate

Code: Select all

 Sub Test3() ' Use Worksheets("    ") Evaluate  ' https://www.eileenslounge.com/viewtopic.php?f=30&t=31883&p=247031#p247035
Dim wb As Workbook, ws As Worksheet
Dim myPath As String, myFile As String
 Let myPath = ThisWorkbook.Path & "\"
 Let myFile = Dir(myPath & "*.xls*")
    Do While myFile <> ""
        If UCase(myFile) <> UCase(ThisWorkbook.Name) Then
         Set wb = Workbooks.Open(myPath & myFile, False)
            For Each ws In wb.Worksheets
                If ThisWorkbook.Worksheets.Item(1).Evaluate("ISREF('" & ws.Name & "'!Hi42)") = False Then
                 ws.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
                End If
            Next ws
            wb.Close SaveChanges:=True
        End If
        myFile = Dir
    Loop
End Sub
I am using Item(1) because you will have at least that first tab worksheet.
But, you can use any worksheet that is in your main file, that is to say, the file in which that code above is in… but that File need not be active for Sub Test3() to work
( Usually when people say "Evaluate" they are talking about Application Evaluate. As we have seen "Evaluate" don't work for what you want to do with it ..... Worksheets(" ") Evaluate is something else...... )
Last edited by Doc.AElstein on 22 Feb 2019, 14:52, edited 1 time in total.
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

YasserKhalil
PlatinumLounger
Posts: 4936
Joined: 31 Aug 2016, 09:02

Re: Using ISREF with closed workbooks

Post by YasserKhalil »

Thanks a lot.

Now everything is working right and everything is clear
Best Regards
Last edited by YasserKhalil on 22 Feb 2019, 14:55, edited 1 time in total.