Macro che esce presto senza errori
Domanda
Sto usando il codice sotto per copiare in tutte le cartelle di lavoro da una particolare cartella in una cartella di lavoro.9 su 10 volte il codice funziona bene e tutti i dati vengono copiati, ma occasionalmente la macro sembra uscire presto senza finitura poiché MsgBox non viene mai visualizzata e non ottengo alcun messaggio di errore.La macro sembra essere stata uscita in quanto mi permette di eseguire altre macro.Qualcuno può consigliarmi cosa potrebbe causare questo?Sembra accadere se inizi a fare altre cose sul tuo computer mentre la macro è in esecuzione.
Sub GetSheets()
Application.ScreenUpdating = False
Dim response
response = MsgBox("This will take some time to run. Are you sure you want to proceed?", vbYesNo)
If response = vbNo Then
Exit Sub
End If
Application.Run ("GetxlsxFiles")
Application.Run ("GetxlsFiles")
DataCopied = 1
Sheets("Instructions").Select
MsgBox "Completed Successfully"
End Sub
Sub GetxlsxFiles()
Dim Sheet As Worksheet
Path = Sheets("Instructions").Range("FileName").Value
Filename = Dir(Path & "*.xlsx")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True, Password:="Password"
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=Workbooks("RSModel.xlsm").Sheets("Current KPIs")
Next Sheet
Workbooks(Filename).Close saveChanges:=False
Filename = Dir()
Loop
End Sub
.
GetXlsFiles Sub è esattamente come quanto sopra tranne l'estensione del file.
Soluzione
Ho re-scritto il tuo codice e fornisci solo commenti lì dentro.
Ci sono troppi per adattarsi al commento.
Ecco il GetxlsxFiles Sub
:
In realtà è breve se rimuovi i commenti che spiega cosa ho fatto.
Sub GetxlsxFiles()
Dim wb As Workbook, wbTemp As Workbook
Dim Path As String, Filename As String ', masterWB As String
Dim Sheet As Worksheet
'~~> Assuming the path is correct
Path = Sheets("Instructions").Range("FileName").Value
'~~> Path should contain e.g. "C:\TestFolder\"
Filename = Dir(Path & "*.xlsx")
'~~> Assuming you are consolidating all sheets
'~~> in the workbook that contain the macro
Set wb = ThisWorkbook
'~~> If not, use the commented line below
'~~> Take note that you do not include the file extension
'Set wb = Workbooks("RSModel")
'~~> Or you can also open it like this
'masterWB = "C:\Foldername\RSModel.xlsm"
'Set wb = Workbooks.Open(Filename:=masterWB, ReadOnly:=True)
Do While Filename <> ""
Set wbTemp = Workbooks.Open(Filename:=Path & Filename, ReadOnly:=True, _
Password:="Password")
For Each Sheet In wbTemp.Sheets
'~~> this adds the sheet after the last sheet in the target WB
'~~> If you specifically want to add it after a specific sheet,
'~~> use the commented line
Sheet.Copy After:=wb.Sheets(wb.Sheets.Count)
'Sheet.Copy After:=wb.Sheets("Current KPIs")
Next
wbTemp.Close False
Filename = Dir
Loop
End Sub
.
Ecco il GetSheets Sub
:
Sub GetSheets()
Application.ScreenUpdating = False
Dim response As Integer
response = MsgBox("This will take some time to run." & vbNewLine & _
"Are you sure you want to proceed?", vbYesNo)
'~~> execute IF in one line
If response = vbNo Then Exit Sub
'~~> No need to use Application.Run. Call the subs directly
GetxlsxFiles
GetxlsFiles
'~~> Not sure what's this for so I commented it
'DataCopied = 1
'~~> If you want below sheet to be selected then
ThisWorkbook.Sheets("Instructions").Select
MsgBox "Completed Successfully", vbInformation
End Sub
.
Penso che sopra dovrebbe essere vicino a quello che vuoi.
Spero che questo aiuti.