Macro non sempre funziona
Hai un problema con Excel? 
Macro non sempre funziona
di Luck (utente non iscritto) data: 21/10/2014 18:29:39
Ciao a tutti, chiedo se potete aiutarmi in quanto non riesco a capire dove sta il problema in questa macro.
Questa macro in excel 2007 funzionava abbastanza bene ma ora in excel 2013 non è più così. Come potete vedere questa macro copia, dal foglio FATTURA di più file da me selezionati, dei range di dati in colonna e li incolla in unico file nel foglio MOVIMENTI in modo consecutivo partendo dalla prima cella vuota.
A volte, soprattutto quando apro per la prima volta il file e aziono la macro, questa funziona correttamente; ma spessissimo, per non dire quasi sempre, questa mi copia solo alcuni dati, "dimenticandosi" di copiare numerosi range di dati e lasciando le caselle vuote.
Il bello è che non si "dimentica" sempre gli stessi dati, la prima volta me li copia e la seconda volta, riprovando con gli stessi file non me li copia.
NON SO PIU' CHE FARE.........CONFIDO NEL VOSTRO AIUTO ED ESPERIENZA.
GRAZIE...................
Option Explicit
Dim file() As String
Private Sub btnPercorso_Click()
Dim F As FileDialog
Dim filename As String
Dim ContTot As Integer
With Me
.txtPercorso = ""
.lstFile.Clear
.txtTotfile = ""
Set F = Application.FileDialog(msoFileDialogFolderPicker)
If F.Show = False Then Exit Sub
.txtPercorso = F.SelectedItems(1)
filename = Dir(Me.txtPercorso & "*.xlsm*", vbArchive Or vbHidden Or vbNormal Or vbReadOnly Or vbSystem)
Do Until filename = ""
ReDim Preserve file(ContTot)
.lstFile.AddItem Left(filename, Len(filename))
file(ContTot) = Left(filename, Len(filename))
ContTot = ContTot + 1
filename = Dir
Loop
.txtTotfile = ContTot
End With
End Sub
Private Sub btnCarica_Click()
Dim F As Variant, pathFile As String
Dim rng As Range
Dim ur As Long, righe As Long, wsdati
If Me.lstFile.ListCount = 0 Then
Call MsgBox("Non hai caricato nessun file excel." & vbCrLf & "" & vbCrLf & _
"Possono essere caricati soltanto i file con estensione .xlsm*" _
, vbExclamation Or vbDefaultButton1, "Selezione_nulla")
Exit Sub
End If
Application.ScreenUpdating = False
Application.DisplayAlerts = False
With wsMovimenti
.Activate
For Each F In file
ur = .Cells(Rows.Count, "H").End(xlUp).Row + 1
If .[H1] = "" Then ur = 1
pathFile = Me.txtPercorso & "" & F
Call read_dati(pathFile, ur)
Next
Unload Me
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "Dati caricati con successo"
End Sub
Function read_dati(filename As String, ur As Long) As Long
Dim app As New Excel.Application, wbk As Excel.Workbook
Dim foglio As Worksheet, righe As Integer
On Error Resume Next
Set wbk = app.Workbooks.Add(filename)
With wbk.Sheets("FATTURA")
.Range("X24:X53").Copy
Range("A" & ur).Select
ActiveSheet.Paste
.Range("X90:X119").Copy
Range("A" & ur + 30).Select
ActiveSheet.Paste
.Range("Y24:Y53").Copy
Range("B" & ur).Select
ActiveSheet.Paste
.Range("Y90:Y119").Copy
Range("B" & ur + 30).Select
ActiveSheet.Paste
.Range("Z24:Z53").Copy
Range("C" & ur).Select
ActiveSheet.Paste
.Range("Z90:Z119").Copy
Range("C" & ur + 30).Select
ActiveSheet.Paste
.Range("B24:B53").Copy
Range("H" & ur).Select
ActiveSheet.Paste
.Range("B90:B119").Copy
Range("H" & ur + 30).Select
ActiveSheet.Paste
.Range("G24:G53").Copy
Range("J" & ur).Select
ActiveSheet.Paste
.Range("G90:G119").Copy
Range("J" & ur + 30).Select
ActiveSheet.Paste
.Range("W24:W53").Copy
Range("L" & ur).Select
ActiveSheet.Paste
.Range("W90:W119").Copy
Range("L" & ur + 30).Select
ActiveSheet.Paste
wbk.Application.CutCopyMode = False
End With
wbk.Close SaveChanges:=False
app.Quit
Set app = Nothing
End Function
|
di lepat (utente non iscritto) data: 21/10/2014 18:54:41
non ci capisci tu che la puoi provare sul documento ed a noi fornisci solo la macro ?
Vuoi Approfondire?