Ciao Seghezzi,
- Alt+F11 per aprire l'editor di VBA
- Alt+IM per inserire un nuovo modulo di codice
- Nel nuovo modulo vuoto, incolla il seguente codice:
'=========>>
Option Explicit
'--------->>
Public Sub Tester()
Dim srcWB As Workbook, destWB As Workbook
Dim oFSO As Object
Dim oFolder As Object
Dim oSubFolder As Object
Dim oFile As Object
Dim destSH As Worksheet, srcSH As Worksheet
Dim srcRng As Range, destRng As Range
Dim LRow As Long
Dim iCtr As Long
Dim CalcMode As Long
Const sPercorso As String = _
"C:\Utenti\Seghezzi\Italia" '<<==== Modifica
Const sCartella As String = "Ordini" '<<==== Modifica
Const sFileRiassuntivo As String = "Riassuntivo.xlsx" '<<==== Modifica
Const sFoglioRiassuntivo As String = "Riepilogo" '<<==== Modifica
Const sNomeFoglio As String = "Sheet1" '<<==== Modifica
Const CelleDaCopiare As String = "A4:K4" '<<==== Modifica
Set destWB = Workbooks.Open(sPercorso & sFileRiassuntivo)
Set destSH = destWB.Sheets(sFoglioRiassuntivo)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sPercorso & sCartella)
On Error GoTo XIT
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
For Each oSubFolder In oFolder.Subfolders
For Each oFile In oSubFolder.Files
Set srcWB = Workbooks.Open(oFile.Path)
With srcWB
Set srcSH = .Sheets(sNomeFoglio)
Set srcRng = srcSH.Range(CelleDaCopiare)
With destSH
LRow = LastRow(destSH, .Columns("A:A"))
Set destRng = .Range("A" & LRow + 1).Resize(1, srcRng.Columns.Count)
End With
srcRng.Copy Destination:=destRng
With destRng
destSH.Hyperlinks.Add Anchor:=.Cells(1, .Columns.Count + 1), _
Address:=oFile.Path, _
TextToDisplay:=srcWB.Name
End With
End With
iCtr = iCtr + 1
srcWB.Close SaveChanges:=False
Next oFile
Next oSubFolder
destWB.Close SaveChanges:=True
XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With
MsgBox Prompt:=iCtr & " file sono stati riepologati", _
Buttons:=vbInformation, _
Title:="REPORT"
End Sub
'--------->>
Public Function LastRow(SH As Worksheet, _
Optional Rng As Range, _
Optional minRow As Long = 1)
If Rng Is Nothing Then
Set Rng = SH.Cells
End If
On Error Resume Next
LastRow = Rng.Find(What:="*", _
after:=Rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
If LastRow < minRow Then
LastRow = minRow
End If
End Function
'<<=========
- Alt+Q per chiudere l'editor di VBA e tornare a Excel
- Salva il file con l’estensione xlsm
- Alt+F8 per aprire la finestra di gestione delle macro
- Seleziona Tester | Esegui
Se non hai familiarità con le macro, ti consiglio il seguente articolo eccellente di Mauro:
http://answers.microsoft.com/it-it/office/wiki/office\_2013\_release-excel/excel-dove-e-come-inserire-il-codice-visual-basic/ed29ee63-a537-4e5d-8631-76766cf40503
===
Regards,
Norman