Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
Ciao Giovanni,
Per affrontare due problemi con il codice suggerito da me, prova invece la seguente versione:
'=========>>
Option Explicit
'--------->>
Public Sub Tester()
Dim srcWB As Workbook, destWB As Workbook
Dim srcSH As Worksheet, destSH As Worksheet
Dim srcRng As Range, copyRng As Range
Dim destRng As Range, RngIntestazioni As Range
Dim arrFile() As Variant
Dim sPrefisso As String, sPercorso As String, sFilename As String
Dim Res As Variant
Dim i As Long, iCtr As Long, iStep As Long
Dim LRow As Long, LCol As Long
Dim CalcMode As Long
Res = InputBox("Quante righe vuoi nei nuovi file?")
If IsNumeric(Res) Then
iStep = CLng(Res)
Else
Call MsgBox( _
Prompt:="Non hai precisato il numero di righe. Riprova!", _
Buttons:=vbInformation, _
Title:="PROBLEMA")
Exit Sub
End If
Set srcWB = ThisWorkbook
With srcWB
Set srcSH = .Sheets(1)
sPercorso = .Path & Application.PathSeparator
sPrefisso = Split(.Name, ".")(0) & "_"
End With
With srcSH
LRow = LastRow(srcSH)
LCol = LastCol(srcSH)
Set srcRng = .Range("A2").Resize(LRow - 1, LCol)
Set RngIntestazioni = srcRng.Rows(0)
End With
For i = 1 To LRow - iStep Step iStep
iCtr = iCtr + 1
Set destWB = Workbooks.Add(xlWBATWorksheet)
Set copyRng = srcRng.Rows(i).Resize(iStep)
Set destSH = destWB.Sheets(1)
Set destRng = destSH.Range("A2")
On Error GoTo XIT
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
With destRng
RngIntestazioni.Copy
With .Cells(0)
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues, , False, False
.PasteSpecial xlPasteFormats, , False, False
End With
copyRng.Copy
With .Cells(1)
.PasteSpecial xlPasteValues, , False, False
.PasteSpecial xlPasteFormats, , False, False
End With
End With
With destWB
sFilename = sPercorso & sPrefisso & Format(iCtr, "00")
.SaveAs sFilename
ReDim Preserve arrFile(1 To iCtr)
arrFile(iCtr) = sFilename
.Close
End With
Next i
Call MsgBox( _
Prompt:="I seguenti file sono stati creati e salvati:" _
& vbNewLine & vbNewLine _
& Join(arrFile, vbNewLine), _
Buttons:=vbInformation, _
Title:="REPORT")
XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With
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
'--------->>
Public Function LastCol(SH As Worksheet, _
Optional Rng As Range)
If Rng Is Nothing Then
Set Rng = SH.Cells
End If
On Error Resume Next
LastCol = Rng.Find(What:="*", _
after:=Rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function
'<<=========
===
Regards,
Norman