Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
Ciao Pankrazio,
La macro funziona alla grande con un unico problema dovuto però alla mia spiegazione iniziale non molto chiara. Infatti mi sono dimenticato di dire che nella colonna matricola ci possono essere valori che iniziano con lo 0, per esempio:
01114556 oppure:
00123488.
In entrambi gli esempi con la traslazione dei dati nel Foglio 2 le matricole perdono gli zero iniziali diventano dunque rispettivamente:
1114556
123488
E' possibile fare qualcosa per correggere il problema?
Prova la seguente versione del codice:
'=========>>
Option Explicit
'--------->>
Public Sub Tester()
Dim WB As Workbook
Dim srcSH As Worksheet, destSH As Worksheet
Dim srcRng As Range, destRng As Range, rCell As Range
Dim arrIn As Variant, arrOut() As Variant
Dim LRow As Long, LCol As Long
Dim i As Long, j As Long, k As Long
Set WB = ThisWorkbook
With WB
Set srcSH = .Sheets("Foglio1")
Set destSH = .Sheets("Foglio2")
End With
With srcSH
LRow = LastRow(srcSH, .Columns("A:A"))
LCol = LastCol(srcSH, .Rows(1))
Set srcRng = .Range("A2").Resize(LRow - 1, LCol)
End With
arrIn = srcRng.Value
For i = 1 To UBound(arrIn, 1)
For j = 4 To UBound(arrIn, 2)
If arrIn(i, j) <> vbNullString Or j = 4 Then
k = k + 1
ReDim Preserve arrOut(1 To 4, 1 To k)
arrOut(1, k) = arrIn(i, 1)
arrOut(2, k) = arrIn(i, 2)
arrOut(3, k) = arrIn(i, 3)
arrOut(4, k) = arrIn(i, j)
End If
Next j
Next i
On Error GoTo XIT
Application.ScreenUpdating = False
With destSH
.Range("A1:D1").Value = Array("Matricola", "Regione", "Nome", "Codice")
With .Range("A2").Resize(k, 4)
.Columns(1).NumberFormat = "@"
.Value = Application.Transpose(arrOut)
End With
End With
XIT:
Application.ScreenUpdating = True
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
'--------->>
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