I'm writing a code to loop through all the files in directory location (specified by the user), and process them to send to our customer.
I'm having trouble with pulling files from the directory. Currently I can't get past the bold line of code. Yesterday I could get to the second bold line (workbooks.open), but got an error message that the file couldn't be located and maybe it had been deleted (it hasn't). I'm not sure what has changed, I was trying several things to get it to work... I thought I had returned it all to where it was when it worked best (to the workbook.open line), but clearly I missed something.
I've been using the debug tool to step through it while watching the myfiles and mypath variables. It seems to be working fine and then can't get past a certain point. I have some of the time savers (application.screenupdating, etc) commented out until I work the kinks out of the code.
I'm hoping for some help from everyone... thank you in advance!
I'm having trouble with pulling files from the directory. Currently I can't get past the bold line of code. Yesterday I could get to the second bold line (workbooks.open), but got an error message that the file couldn't be located and maybe it had been deleted (it hasn't). I'm not sure what has changed, I was trying several things to get it to work... I thought I had returned it all to where it was when it worked best (to the workbook.open line), but clearly I missed something.
I've been using the debug tool to step through it while watching the myfiles and mypath variables. It seems to be working fine and then can't get past a certain point. I have some of the time savers (application.screenupdating, etc) commented out until I work the kinks out of the code.
I'm hoping for some help from everyone... thank you in advance!
Code:
Sub PrepareForCustomerWeightInspection()
Dim question As Integer
Dim mypath As String
Dim question2 As Integer
Dim myfiles As String
Dim counttotal As Integer
Dim count As Integer
Dim wssheet As Worksheet
Dim question3 As Integer
'Application.Calculation = xlCalculationManual
'Application.ScreenUpdating = False
'
'
'
'Prepares Weight Inspection sheets for customer, loops through multiple files in a single directory
'
'
'
'ask user if they are sure they want to
'
question = MsgBox("Are you sure you want to begin processing Weight Inspection Reports?" & _
vbCrLf & vbCrLf & "This action cannot be undone." & vbCrLf & vbCrLf & _
"Always keep a backup of files before processing.", vbYesNo + vbQuestion)
If question = vbYes Then
GoTo next1
Else
MsgBox "Okay, maybe next time."
Exit Sub
End If
'
'
'
'
'
'
next1:
MsgBox "Please select the folder location that contains the Weight Inspection Reports you want to process." & vbCrLf & vbCrLf & "Make sure the folder contains ONLY Weight Inspection documents."
'
'
'
' opens dialog box for user to select directory location
'
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Select location of documents"
.Show
If .SelectedItems.count = 0 Then
MsgBox "Canceled."
Exit Sub
Else
mypath = .SelectedItems(1)
question2 = MsgBox("Selected location is: " & mypath & vbCrLf & vbCrLf & "Are you sure this is the correct location?", vbYesNo + vbExclamation)
If question2 = vbYes Then
GoTo next2
Else
Exit Sub
End If
End If
End With
'
'
'
'
'
'
next2:
'
'
'
'loops through and counts files, used for files counter in status bar
'
myfiles = Dir(mypath & "\*.xlsm")
counttotal = 0
Do While myfiles <> ""
counttotal = counttotal + 1
[B]myfiles = Dir() [/B]'code can't get past this point currently
Loop
question3 = MsgBox("There are " & counttotal & " total files to process. Please be patient.", vbOKCancel + vbExclamation)
If question3 = vbOK Then
GoTo next3
Else
Exit Sub
End If
'
'
'
'
'
'
'
next3:
'
'
'loops through files, opens and processes each file
'
count = 0
myfiles = Dir(mypath & "\" & "*.xlsm")
Do While myfiles <> ""
Application.StatusBar = "Processing... " & count & " of " & counttotal & " files"
[B]Workbooks.Open myfiles [/B]'code previously made it to this point, but no longer can
Application.Run ("" & myfiles & "!togglecutcopypaste(True)") ' macro enables copy and paste in open file
Sheets("Weight Inspection Report").Unprotect Password:="pass1"
Sheets("Master List").Visible = xlSheetVisible
Sheets("Weight Inspection Report").Activate
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Rows("1:2").Delete Shift:=xlUp
Columns("M:M").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Delete Shift:=xlToLeft
Sheets(Array("Staging Stack A", "Stack B", "Stack C", "Stack D", "Stack E", "Master List")).Delete
ActiveWorkbook.Save
ActiveWorkbook.Close savechanges:=False
myfiles = Dir()
count = count + 1
Loop
'Application.ScreenUpdating = True
'Application.Calculation = xlCalculationAutomatic
Application.StatusBar = False
End Sub