Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
Ciao Marco,
nessuno riesce a darmi una mano?
speravo fosse una questione meno complicata :(
Non ho risposto prima perché, sfortunatamente, mi ero sfuggito la tua ultima risposta (:-
Ora ho scaricato i tuoi file e ti suggerirei di sostituire il mio codice precedente con 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, destRng As Range
Dim arrDati() As Variant, arrIn As Variant, arrNuoviProdotti() As Variant, arrTemp As Variant
Dim Res As Variant
Dim oTable As ListObject
Dim sStr As String, sPath As String, sDate As String
Dim sFilename As String, sProdotto As String
Dim iFileDate 'As Long
Dim i As Long, j As Long, iCtr As Long
Dim iCol As Long, jCol As Long
Dim iRow As Long, jRow As Long
Const sFile\_Vendite\_Giornaliere As String = **"AB Seller - ERRANI MARCO\_D0194.xlsx" '<<=== Modifica**
Const sFoglio\_Sorgente As String = **"FT\_DIVISIONE" '<<=== Modifica**
Const sPercorso As String = **"C:\dati\" '<<=== Modifica**
Const iPrimaRiga\_Prodotti As Long = **9**
Const sPrefisso\_Data As String = **"Fatturato progressivo al "**
Const sCella\_Data As String = **"C7"**
sStr = Application.PathSeparator
If Right(sPercorso, 1) <> sStr Then
sPath = sPercorso & sStr
Else
sPath = sPercorso
End If
On Error GoTo XIT
Application.ScreenUpdating = False
sFilename = sPath & sFile\_Vendite\_Giornaliere
Set srcWB = Workbooks.Open(sFilename)
Set srcSH = srcWB.Sheets(sFoglio\_Sorgente)
With srcSH
iRow = LastRow(srcSH, .Columns("A:C"))
Set srcRng = .Range("A:C").Resize(iRow - iPrimaRiga\_Prodotti + 1).Offset(iPrimaRiga\_Prodotti - 1)
arrIn = srcRng.Value
sDate = Mid(.Range(sCella\_Data).Value, Len(sPrefisso\_Data) + 1)
iFileDate = CLng(DateSerial(Right(sDate, 4), Left(sDate, 2), Mid(sDate, 4, 2)))
.Parent.Close SaveChanges:=False
End With
Set destWB = ThisWorkbook
Set destSH = destWB.Sheets(1)
With destSH
jRow = LastRow(srcSH, .Columns("A"))
jCol = LastCol(destSH)
Set destRng = .Range("A2").Resize(jRow - 1, jCol)
arrDati = destRng.Value2
End With
ReDim Preserve arrDati(1 To UBound(arrDati), 1 To UBound(arrDati, 2) + 1)
iCol = UBound(arrDati, 2)
arrDati(1, iCol) = iFileDate
For i = 1 To UBound(arrIn)
sProdotto = arrIn(i, 1)
arrTemp = Application.Index(arrDati, 0, 1)
Res = Application.Match(sProdotto, arrTemp, 0)
If Not IsError(Res) Then
arrDati(Res, iCol) = arrIn(i, 3)
Else
'\\ Nuovo prodotto!
iCtr = iCtr + 1
ReDim Preserve arrNuoviProdotti(1 To iCol, 1 To iCtr)
arrNuoviProdotti(1, iCtr) = arrIn(i, 1)
arrNuoviProdotti(iCol, iCtr) = arrIn(i, 3)
End If
Next i
With destRng
With .Cells(1).Resize(UBound(arrDati), iCol)
.Value = arrDati
.Rows(1).NumberFormat = "dd/mm/yy"
End With
If CBool(iCtr) Then
'\\ Nuovi prodotti trovati
.Cells(jRow, 1).Resize(iCtr, iCol).Value = Application.Transpose(arrNuoviProdotti)
End If
End With
Call MsgBox(Prompt:="Fatto", \_
Buttons:=vbInformation, \_
Title:="REPORT")
XIT:
Application.ScreenUpdating = True
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
'<<========
Potresti scaricare il mio file di prova Marco20211116.xlsm
Per evitare problemi con l'attuale editor del forum, che inserisce righe vuote indesiderate quando il codice pubblicato viene ricopiato, copia il codice direttamente dal mio file di prova nel tuo file.
===
Regards,
Norman