Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
mi dice:
Error 13 (Tipo non corrispondente) alla riga230 nella routine: Tester
Ciao Filippo,
Potrei replicare il tuo errore se non dovessi dichiare la variabile Res come tipo Variant
Dim Res As Variant
Pertanto mi chiedo se tu abbia incollato il mio codice e, più in particolare, se il tuo codice includa una copia esatta delle righe suggerite:
'==========>>
Option Explicit
'---------->>
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet, destSH As Worksheet
Dim srcRng As Range, destRng As Range, headerRng As Range
Dim LRow As Long, LCol As Long
Dim arrEscludere As Variant
Dim Res As Variant
Dim i As Long
Dim CalcMode As Long
Dim blData As Boolean, blHeader As Boolean
Const sCartella As String = "Pippo.xlsx" '<<==== Modifica
Const sName As String = "Riepilogo" '<<==== Modifica
Tuttavia, dato che non hai la necessità di escludere qualsiasi dei fogli, prova a sostituire il codice precedente con la seguente versione semplificata:
'==========>>
Option Explicit
'---------->>
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet, destSH As Worksheet
Dim srcRng As Range, destRng As Range, headerRng As Range
Dim LRow As Long, LCol As Long
Dim arrEscludere As Variant
Dim CalcMode As Long
Dim blData As Boolean, blHeader As Boolean
Const sCartella As String = "Pippo.xlsx" '<<==== Modifica
Const sName As String = "Riepilogo" '<<==== Modifica
10 On Error GoTo ErrHandler
20 With Application
30 CalcMode = .Calculation
40 .Calculation = xlCalculationManual
50 .ScreenUpdating = False
60 End With
70 Set WB = Workbooks(sCartella)
80 With WB
90 If SheetExists(sName, WB) Then
100 Set destSH = .Sheets(sName)
110 destSH.UsedRange.EntireRow.Delete
120 Else
130 Set destSH = .Sheets.Add(Before:=.Sheets(1))
140 destSH.Name = sName
150 End If
160 For Each SH In .Worksheets
170 With SH
180 If Not .Name = sName Then
190 If headerRng Is Nothing Then
200 Set headerRng = .UsedRange.Rows(1)
210 End If
220 LRow = LastRow(SH, .UsedRange)
230 LCol = LastCol(SH, .UsedRange)
240 blData = LRow > 1
250 If blData Then
260 Set srcRng = Intersect(.UsedRange, .Rows(2)).Resize(LRow - 1, LCol)
270 If Not blHeader Then
280 With destSH
290 headerRng.Copy Destination:=.Range("A1")
300 Set destRng = .Range("A2")
310 End With
320 blHeader = True
330 End If
340 srcRng.Copy Destination:=destRng
350 Set destRng = destRng.Offset(LRow - 1)
360 End If
370 End If
380 End With
390 Next SH
400 End With
XIT:
410 On Error Resume Next
420 With Application
430 .Calculation = CalcMode
440 .ScreenUpdating = True
450 End With
460 On Error GoTo 0
470 Exit Sub
ErrHandler:
480 Call MsgBox(Prompt:="Error " _
& Err.Number _
& " (" _
& Err.Description _
& ") alla riga " & Erl _
& " nella routine: Tester", _
Buttons:=vbCritical, _
Title:="ERRORE")
490 Resume XIT
End Sub
'---------->>
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
'---------->>
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
'---------->>
Function SheetExists(aStr As String, Optional aWB As Workbook) As Boolean
If aWB Is Nothing Then
Set aWB = ActiveWorkbook
End If
On Error Resume Next
SheetExists = CBool(Len(aWB.Sheets(aStr).Name))
On Error GoTo 0
End Function
'<<==========
===
Regards,
Norman