VBA code giving errors and stopping Excel

steve400243

Active Member
Joined
Sep 15, 2016
Messages
285
Hello Experts, I have this code being used to loop through files in a file path, and copy specific data to another sheet. It is giving me several problems when trying to run it. Was hoping someone could look at it and see any errors or suggestions they may have to make it run better? thanks for any help provided.

Code:
Sub t()
   Dim wb As Workbook, sh As Worksheet, ary As Variant, fPath As String, fName As String, i As Long, rw As Long
  
   Application.ScreenUpdating = False
  
   fPath = "X:\SEA Shares\warehouse\CFS and FMM Program\SEA Devanned February-2020\"

   Set sh = ThisWorkbook.Sheets("Sheet1")
   ary = Array("C3", "C4", "C5", "H2", "H3", "H4")
  
   fName = Dir(fPath & "*.xls*")
   Do While fName <> ""
      Application.StatusBar = "Please be patient... processing: " & fName
      If fName <> ThisWorkbook.Name Then
      
         Set wb = Workbooks.Open(fPath & fName)
        
         'Header (Optional)
         rw = LastRow(sh.Range("B60000:M60000")) + 1
         ThisWorkbook.Sheets("Param").Range("B1:M3").Copy sh.Cells(rw, 2)
        
         'Data 1
         rw = LastRow(sh.Range("B60000:M60000")) + 1
         For i = 2 To 7
            sh.Cells(rw, i) = wb.Sheets(1).Range(ary(i - 2)).Value
         Next i
        
         'Data 2 (Paste Values only)
         With wb.Sheets(1)
            .Range("A13:A35").Copy
            sh.Cells(rw, 8).PasteSpecial xlPasteValues

            .Range("G13:G35").Copy
            sh.Cells(rw, 9).PasteSpecial xlPasteValues

            .Range("H13:H35").Copy
            sh.Cells(rw, 10).PasteSpecial xlPasteValues

            .Range("J13:J35").Copy
            sh.Cells(rw, 11).PasteSpecial xlPasteValues

            .Range("K13:K35").Copy
            sh.Cells(rw, 12).PasteSpecial xlPasteValues
            
            .Range("D13:D35").Copy
            sh.Cells(rw, 13).PasteSpecial xlPasteValues
         End With
        
         wb.Close False
      End If
      fName = Dir
   Loop
  
   Application.StatusBar = False
   Application.ScreenUpdating = True
End Sub

'Find last row when looking at multiple columns. Return 99999 = error.
Function LastRow(rg As Range) As Long
   Dim c As Range
   If rg.Cells.Count > 100 Then LastRow = 99999: Exit Function
   LastRow = 0
   For Each c In rg
      If c.End(xlUp).Row > LastRow Then
         LastRow = c.End(xlUp).Row
      End If
   Next c
End Function
 

Some videos you may like

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Caleeco

Well-known Member
Joined
Jan 9, 2016
Messages
899
Hi Steve

What line do you get an error on?

It may also help if you can explain what you're trying to achieve overall. There may be a simpler way to do what you need.

Caleeco
 

steve400243

Active Member
Joined
Sep 15, 2016
Messages
285
It does not give an error on the code. My excel just locks up and then has to be reset. It also seems to take a long time to run. Overall this opens every file in the in the file path, and copies specific data from each file and builds a report. Separated by the header that I have in another sheet "Param". kind of hard to explain, but below is the desired results.
MBL - DEVANNED is from cells "C3", "C4", "C5", "H2", "H3", "H4" in the files that are opened
and the other data is from the other ranges noted in the code. Thanks for looking at it.


MBLVESSELCONTAINERRELEASEDRECEIVEDDEVANNEDHAWBCUSTOMS OBLDELIVERY ORDERSHIPPEDMODE
CMDUCNCT427114CMA CGM CALLISTOTGHU89031552/3/20202/3/20202/3/2020SHS0002099432/6/2020TELEX1/31/20202/6/2020FMM
SHS0002100691/21/2020TELEX1/24/20202/4/2020FMM
SHS0002102681/30/20202/7/20201/30/20202/7/2020FMM
SHS0002103852/3/20202/3/20202/3/20202/4/2020FMM
SHS0002103882/7/2020TELEX2/6/20202/7/2020
SHS0002104152/5/2020TELEX1/23/20202/5/2020FMM
SHS0002105182/3/20202/3/20202/3/20202/4/2020FMM
SHS0002105641/31/2020TELEX1/31/20202/4/2020FMM
SHS0002105862/3/20202/3/20202/3/20202/4/2020FMM
SHS0002105881/24/20201/24/20201/24/20202/4/2020FMM
SHS0002105931/28/2020TELEX1/29/20202/4/2020FMM
SHS0002102142/4/2020TELEX1/21/20202/4/2020FMM
SHS0002103101/29/2020TELEX1/23/20202/4/2020FMM
SHS0002105821/31/2020EXPRESS1/31/20202/4/2020FMM
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
10,199
Office Version
2007
Platform
Windows
Here a macro with some improvements for you to consider.
With this code, the LastRow function is no longer necessary.

Note: Sheet1 must have at least one row with data in column B.

VBA Code:
Sub t()
  Dim wb As Workbook, sh As Worksheet, ary As Variant, fPath As String, fName As String, i As Long, rw As Long
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  fPath = "X:\SEA Shares\warehouse\CFS and FMM Program\SEA Devanned February-2020\"

  Set sh = ThisWorkbook.Sheets("Sheet1")
  ary = Array("C3", "C4", "C5", "H2", "H3", "H4")
  
  fName = Dir(fPath & "*.xls*")
  Do While fName <> ""
    Application.StatusBar = "Please be patient... processing: " & fName
    If fName <> ThisWorkbook.Name Then
    
      Set wb = Workbooks.Open(fPath & fName)
      
      'Header (Optional)
      rw = sh.Range("B:M").Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
      ThisWorkbook.Sheets("Param").Range("B1:M3").Copy sh.Cells(rw, 2)
      
      'Data 1
      rw = sh.Range("B:M").Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
      For i = 2 To 7
         sh.Cells(rw, i) = wb.Sheets(1).Range(ary(i - 2)).Value
      Next i
     
      'Data 2 (Paste Values only)
      With wb.Sheets(1)
        .Range("A13:A35, G13:G35, H13:H35, J13:J35, K13:K35, D13:D35").Copy
        sh.Cells(rw, 8).PasteSpecial xlPasteValues
      End With
      
      wb.Close False
    End If
    fName = Dir
  Loop
  
  Application.StatusBar = False
  Application.ScreenUpdating = True
End Sub
 

steve400243

Active Member
Joined
Sep 15, 2016
Messages
285
Thank for that Dante, seems to work faster, but now i'm getting an "Automation Error Exception Occurred" In msft visual basic for applications in the middle of it running and it locks up excel.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
10,199
Office Version
2007
Platform
Windows
The process copied some books?

I guess you have problems with one of the books.
Try the following.
First, put the macro and the "Sheet1" and "param" sheets in a new book.
Try the following macro, which will save the book every time a file is processed, that way you can check which book has problems opening.

Rich (BB code):
Sub t()
  Dim wb As Workbook, sh As Worksheet, ary As Variant, fPath As String, fName As String, i As Long, rw As Long
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  fPath = "X:\SEA Shares\warehouse\CFS and FMM Program\SEA Devanned February-2020\"

  Set sh = ThisWorkbook.Sheets("Sheet1")
  ary = Array("C3", "C4", "C5", "H2", "H3", "H4")
  
  fName = Dir(fPath & "*.xls*")
  Do While fName <> ""
    Application.StatusBar = "Please be patient... processing: " & fName
    If fName <> ThisWorkbook.Name Then
    
      Set wb = Workbooks.Open(fPath & fName)
      
      'Header (Optional)
      rw = sh.Range("B:M").Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
      ThisWorkbook.Sheets("Param").Range("B1:M3").Copy sh.Cells(rw, 2)
      
      'Data 1
      rw = sh.Range("B:M").Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
      For i = 2 To 7
         sh.Cells(rw, i) = wb.Sheets(1).Range(ary(i - 2)).Value
      Next i
     
      'Data 2 (Paste Values only)
      With wb.Sheets(1)
        .Range("A13:A35, G13:G35, H13:H35, J13:J35, K13:K35, D13:D35").Copy
        sh.Cells(rw, 8).PasteSpecial xlPasteValues
      End With
      
      wb.Close False

      ThisWorkbook.Save

    End If
    fName = Dir
  Loop
  
  Application.StatusBar = False
  Application.ScreenUpdating = True
End Sub
 

steve400243

Active Member
Joined
Sep 15, 2016
Messages
285
With that setup i didn't get the Automation error, but "Excel has stopped working" is the new issue.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
10,199
Office Version
2007
Platform
Windows
Ok, you can check the last saved excel and analyze which was the last book copied, that way you will know if any of the books pending copying has the problem.
Or start discarding, remove the books that were copied from the folder and run the macro again, until you find which or which books at the time of opening are stopping to excel.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
10,199
Office Version
2007
Platform
Windows
Glad we could help & thanks for the feedback
 

Forum statistics

Threads
1,089,518
Messages
5,408,755
Members
403,224
Latest member
rholmesa

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top