+ Reply to Thread
Results 1 to 6 of 6

Need MsgBox in On Error

Hybrid View

  1. #1
    Registered User
    Join Date
    09-21-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    21

    Need MsgBox in On Error

    Hi,

    I have a code that loops through a range, opening workbooks and copying them over. In some cases the workbook names in the loop will not exist. I'd like a message box to pop up with "File not Found" should this even occur, but I don't know where to put it with the On Error statement. Here's what I have, can someone help me by adding the message box?
    Set rng = Range("A4:A5")
    
    For Each row In rng.Rows
      For Each cell In row.Cells
      
      On Error Resume Next
      
    Dim errors1 As String
    
    errors1 = "C:\asdf\test\" & Format(cell.Value) & ".csv"
    Workbooks.Open errors1, True, True
    
    ThisWorkbook.Sheets(cell.Value).Range("A1:Z50000").Value = ActiveWorkbook.ActiveSheet.Range("A1:Z50000").Value
    
    ActiveWorkbook.Close False
    Next cell
    Next row
    Thanks

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Need MsgBox in On Error

    hi DAK266,

    ...
    errors1 = "C:\asdf\test\" & Format(cell.Value) & ".csv"
    Workbooks.Open errors1, True, True
    
    If Err.Number = 1004 Then
        MsgBox "File is not found", vbCritical, "File error"
        Err.Clear
    End If
    
    ThisWorkbook.Sheets(cell.Value).Range("A1:Z50000").Value = ActiveWorkbook.ActiveSheet.Range("A1:Z50000").Value
    ....

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Need MsgBox in On Error

    Hi DAK266

    Modify your Code to this
    Set Rng = Range("A4:A5")
    
        For Each Row In Rng.Rows
            For Each cell In Row.Cells
    
                On Error Resume Next
    
                Dim errors1 As String
    
                errors1 = "C:\asdf\test\" & Format(cell.Value) & ".csv"
                
                If FileFolderExists(errors1) Then
                    Workbooks.Open errors1, True, True
    
                    ThisWorkbook.Sheets(cell.Value).Range("A1:Z50000").Value = ActiveWorkbook.ActiveSheet.Range("A1:Z50000").Value
    
                    ActiveWorkbook.Close False
                Else
                    MsgBox errors1 & " does not exist"
                End If
            Next cell
        Next Row
    and add this Function
    Public Function FileFolderExists(strFullPath As String) As Boolean
    'Author       : Ken Puls (www.excelguru.ca)
    'Macro Purpose: Check if a file or folder exists
    
        On Error GoTo EarlyExit
        If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
    
    EarlyExit:
        On Error GoTo 0
    
    End Function
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Need MsgBox in On Error

    on the other hand what is the purpose of the message box if on OK click the code would continue. It would be more logical to collect all errors and show msgbox on code completion with processing results

  5. #5
    Registered User
    Join Date
    09-21-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Need MsgBox in On Error

    Jaslake, thank you--it worked perfectly!

    Watersev, thanks for the suggestion--I already have that logic in the actual worksheet.

    Thank you both for the help.

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Need MsgBox in On Error

    You're welcome...glad I could help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How to make an error come up as a msgbox
    By Lee_wwfc in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-23-2013, 01:33 PM
  2. error with MsgBox
    By ramserp in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-10-2009, 06:39 AM
  3. msgbox appear with error msg
    By jrd269 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-02-2005, 12:05 PM
  4. [SOLVED] msgbox on error
    By Ciara in forum Excel General
    Replies: 6
    Last Post: 05-26-2005, 05:15 PM
  5. MsgBox Code Error
    By MBlake in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-02-2005, 07:06 PM

Tags for this Thread

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