Ciao Claudio,
Prova qualcosa del genere:
- 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 StampaDati()
Dim WB As Workbook
Dim SH As Worksheet, newSH As Worksheet
Dim Rng As Range, Rng2 As Range, rngPrint As Range
Dim rCell As Range, rngHeaders As Range
Dim i As Long, iLastRow As Long
Dim CalcMode As Long
Const sFoglio As String = "Foglio1" '<<==== Modifica
Const sColonnaEseguito As String = "P" '<<==== Modifica
Const sParola As String = "Da Eseguire"
'<<==== Modifica
Const NumeroDiColonne As Long = 16 '<<==== Modifica
Const sNewSheetName As String = "TempPrint"
Set WB = ThisWorkbook
Set SH = WB.Sheets(sFoglio)
With SH
iLastRow = LastRow(SH, .Columns("A:A"))
Set Rng = .Range("A1:A" & iLastRow)
End With
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
For i = 2 To iLastRow Step 2
Set rCell = Rng.Cells(i, sColonnaEseguito)
With rCell
If UCase(.Value) = UCase(sParola) Then
If Rng2 Is Nothing Then
Set Rng2 = rCell.Resize(2)
Else
Set Rng2 = Union(rCell.Resize(2), Rng2)
End If
End If
End With
Next i
If Not Rng2 Is Nothing Then
Set rngPrint = Intersect(Rng2.EntireRow, SH.UsedRange)
Set rngHeaders = Rng.Rows(1).Resize(1, NumeroDiColonne)
With WB
On Error Resume Next
Set newSH = .Sheets(sNewSheetName)
On Error GoTo 0
If Not newSH Is Nothing Then
newSH.UsedRange.ClearContents
Else
Set newSH = .Worksheets.Add(Before:=.Sheets(1))
newSH.Name = sNewSheetName
End If
End With
With newSH
rngHeaders.Copy Destination:=.Range("A1")
rngPrint.Copy newSH.Range("A2")
.UsedRange.EntireColumn.AutoFit
.PrintPreview 'Out
End With
End If
XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With
End Sub
'--------->>
Public Function LastRow(SH As Worksheet, _
Optional Rng As Range)
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
End Function
'<<=========
- Alt-Q per chiudere l'editor di VBA e tornare a Excel.
- Alt-F8 per aprire la finestra di gestione delle macro
- Seleziona StampaDati | Esegui
===
Regards,
Norman