Results 1 to 4 of 4

loop counter not recognised

Threaded View

  1. #1
    Forum Contributor Dave H9's Avatar
    Join Date
    03-10-2011
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    418

    loop counter not recognised

    Please can someone help with the below code? I am trying to write this to check whether a workbook is already open by another user but it gets stuck in the loop and ignores (or more probably my code is wrong) the counter which should cause an "Exit Do" at a value of two -

    Sub Rd_Nly_Trial()
    
        Dim strWorkbookPath2 As String
        Dim strWorkbookName2 As String
        Dim Counter As Variant
            
        strWorkbookPath2 = "P:\General\"
        strWorkbookName2 = "logger.xls"
        
        Workbooks.Open (strWorkbookPath2 & strWorkbookName2)
        
        If ActiveWorkbook.ReadOnly = True Then
            Response = MsgBox("File is in use on another machine", vbAbortRetryIgnore, "Warning")
            If Response = vbAbort Then
                ActiveWorkbook.Saved = True
                ActiveWorkbook.Close
                MsgBox "Please try again later", vbInformation, "Information not updated"
                Exit Sub
            ElseIf Response = vbRetry Then
                Counter = 0
                Do    ' Outer loop.
                    Do While ActiveWorkbook.ReadOnly = True
                        If ActiveWorkbook.ReadOnly = True Then    ' If condition is True.
                            ActiveWorkbook.Saved = True
                            ActiveWorkbook.Close
                        ElseIf Counter = 2 Then
                            MsgBox "Failed to save information, try again later", vbCritical, "Warning"
                            Exit Do    ' Exit inner loop.
                        Else
                            Exit Do
                        End If
                        Counter = Counter + 1
                        ActiveSheet.Range("A2").Value = Counter
                        Workbooks.Open (strWorkbookPath2 & strWorkbookName2)
                    Loop
                Loop Until ActiveWorkbook.ReadOnly = False   ' Exit outer loop immediately.
            Else
                MsgBox "Please enter information manually when file is available", vbExclamation
            End If
                
            
        Else
        
        End If
    
        'I will be calling another macro here!
    
    End Sub
    Also I realise this is perhaps the wrong approach for my solution. Is there a way of determing read only status without opening the workbook?

    Many thanks

    Dave
    Last edited by Dave H9; 05-13-2011 at 02:53 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1