Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
Ciao Geacs,
Ciao Norman, ancora una volta sei stato impeccabile. Ho provato il codice e fa esattamente quello che ho chiesto nella domanda iniziale. Una sola cosa ho riscontrato da correggere, quando nella combobox non c'è nessun valore se scelgo il formato pdf sul file che crea vedo solo le righe d'intestazione, se scelgo excel la cartella creata visualizza le prime 2 righe e il filtro attivo che nasconde tutte le righe con i valori. Spero di aver spiegato bene quello che non va.
Colpa mia - avevo trascurato il caso della ComboBox vuota!
Sostituisci il codice nel modulo standard con la seguente versione nella quale le modifice sono evidenziate in grassetto:
'=========>>
Option Explicit
Public vArrCriteri() As Variant
Public Const sColonna As String = "G"
Public Const iRigaIntestazioni As Long = 2
Public Const sFoglio As String = "Archivio"
Public Const sPercorso As String = _
"C:\Users\ndj\Documents" '<<=== Modifica
'--------->>
Public Sub CreaElenco()
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range, rCell As Range
Dim oDic As Object
Dim vArr As Variant
Dim sStr As String
Dim i As Long, LRow As Long
Dim CalcMode As Long
Set WB = ThisWorkbook
Set SH = WB.Sheets(sFoglio)
With SH
LRow = LastRow(SH, .Columns(sColonna))
Set Rng = .Range(sColonna & iRigaIntestazioni + 1). _
Resize(LRow - iRigaIntestazioni)
End With
vArr = Rng.Value
Set oDic = CreateObject("Scripting.Dictionary")
With oDic
For i = 1 To UBound(vArr)
sStr = vArr(i, 1)
If Not .exists(sStr) Then
.Add Key:=sStr, Item:=vbNullString
End If
Next i
End With
vArrCriteri = oDic.keys
XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With
End Sub
'--------->>
Public Sub CreaPdf(sCriterio As String)
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Dim sStr As String
Dim LRow As Long
Dim OrientationMode As Long
Set WB = ThisWorkbook
Set SH = WB.Sheets(sFoglio)
With SH
If Not sCriterio = vbNullString Then
Set Rng = .Range(sColonna & 1)
sStr = Rng(2).Value
Rng.CurrentRegion.AutoFilter _
Field:=Rng.Column, _
Criteria1:=sCriterio, _
Operator:=xlOr, _
Criteria2:="=" & sStr
End If
With .PageSetup
OrientationMode = .Orientation
.Orientation = xlLandscape
End With
On Error GoTo XIT
Application.ScreenUpdating = False
.Visible = xlSheetVisible
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=sPercorso & SH.Name _
& Format(Now, "yyyymmdd hh-mm") & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=False
Rng.AutoFilter
.PageSetup.Orientation = OrientationMode
End With
XIT:
SH.Visible = xlSheetVeryHidden
Application.ScreenUpdating = True
End Sub
'--------->>
Public Sub CreaFileExcel(sCriterio As String)
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Dim sStr As String
Dim LRow As Long
Dim OrientationMode As Long
Set WB = ThisWorkbook
Set SH = WB.Sheets(sFoglio)
With SH
If Not sCriterio = vbNullString Then
Set Rng = .Range(sColonna & 1)
sStr = Rng(2).Value
Rng.CurrentRegion.AutoFilter _
Field:=Rng.Column, _
Criteria1:=sCriterio, _
Operator:=xlOr, _
Criteria2:="=" & sStr
With .PageSetup
OrientationMode = .Orientation
.Orientation = xlLandscape
End With
End If
On Error GoTo XIT
Application.ScreenUpdating = False
.Visible = xlSheetVisible
.Copy
With ActiveWorkbook
.SaveAs Filename:=sPercorso & SH.Name _
& Format(Now, "yyyymmdd hh-mm") & "xlsx", _
FileFormat:=51
.Close SaveChanges:=False
End With
Rng.AutoFilter
.PageSetup.Orientation = OrientationMode
End With
XIT:
SH.Visible = xlSheetVeryHidden
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
'<<=========
Ho aggiornato il mio file di prova, che troverai sempre allo stesso link.
===
Regards,
Norman