Ciao Nicola,
ho messo su, adattando alle mie esigenze il sottoelencato codice di Mauro Gamberini che mi permette di copiare dei dati contenuti in un range definito dal foglio 1 al foglio 2.
Il problema è che se pur funzionante penso di aver creato un codice ridondante e disordinato, cioe:
se metto queste due righe il codice funziona :
Sheets("Foglio1").Select
With Worksheets("Foglio1")
se lascio With Worksheets("Foglio1") il codice va in errore, è come se non trovasse il foglio 2 già selezionato.
Come ho imparato da voi seguendo il forum penso che nelle due righe in cui richiamo il Foglio2 ho pisticciato un po.
Mi aiutate a capire e a migliorare il codice in argomento.
Questo è il codice che utilizzo:
Public Sub CopiaeIncolla()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim LRiga As Long
LRiga = Sheets("Foglio1").Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
With ThisWorkbook
Set sh1 = .Worksheets("Foglio1")
Set sh2 = .Worksheets("Foglio2")
End With
With sh1
sh2.Cells.Clear
.Range("C1:M" & LRiga).Copy _
sh2.Range("A1").PasteSpecial xlPasteValues
Application.CutCopyMode = False
Sheets("Foglio2").Select
With Worksheets("Foglio2")
.Columns("A:A").NumberFormat = "m/d/yyyy"
.Columns("A:K").Select
.Columns("A:K").EntireColumn.AutoFit
.Columns("C:C").NumberFormat = "€ #,##0.00"
Cells.Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End With
End With
Set sh2 = Nothing
Set sh1 = Nothing
End Sub
Solitamente è nè necessario nè efficiciente fare delle selezioni di fofgli o di intervalli, Quindi prova il seguente adattamento del tuo codice:
'=========>>
Option Explicit
'--------->>
Public Sub CopiaeIncolla()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim srcRng As Range, destRng As Range
Dim LRiga As Long
On Error GoTo XIT
Application.ScreenUpdating = False
With ThisWorkbook
Set sh1 = .Worksheets("Foglio1")
Set sh2 = .Worksheets("Foglio2")
End With
With sh1
LRiga = .Range("A" & Rows.Count).End(xlUp).Row
Set srcRng = .Range("C1:M" & LRiga)
End With
With sh2
.Cells.Clear
Set destRng = .Range("A1").Resize(LRiga, srcRng.Columns.Count)
End With
srcRng.Copy
With destRng
.PasteSpecial xlPasteValues
.Columns("A:A").NumberFormat = "m/d/yyyy"
.Columns("C:C").NumberFormat = "€ #,##0.00"
.EntireColumn.AutoFit
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
XIT:
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
Set sh2 = Nothing
Set sh1 = Nothing
End Sub
'<<=========
Postscriptum:
Ho modificato il codice
===
Regards,
Norman
