
Sub inserisci_transazioni()
Dim risposta
Dim file_calc, foglio_calc, file_temp, foglio_temp
Dim i, k, fine, valore As Long
Dim lRow As Long
Dim ItemRow1, ItemRow2 As String
Dim lengthRow1, lengthRow2 As String
Set file_calc = Workbooks("Transazioni per servizio.xlsm")
Set foglio_calc = file_calc.Worksheets("DB")
foglio_calc.Activate
Rows("2:1048000").Select
Selection.Delete Shift:=xlUp
Selection.ClearContents
risposta = vbYes
While risposta = vbYes
risposta = MsgBox("Vuoi caricare un altro mese?", vbYesNo, "seleziona file")
If risposta = vbYes Then
Percorso = Excel.Application.GetOpenFilename("File Excel (*.xlsx; *.xls), *.xlsx; *.xls", , "Selezionare il file Excel CPU", "Apri", "False")
Workbooks.Open Filename:=Percorso
Set file_temp = Workbooks(ActiveWorkbook.Name)
Set foglio_temp = file_temp.Worksheets(ActiveSheet.Name)
fine = foglio_temp.Range("a1048576").End(xlUp).Row
inizio = foglio_calc.Range("a1048576").End(xlUp).Row
i = fine
k = inizio + 1
While i > 1
foglio_calc.Cells(k, 1) = foglio_temp.Cells(i, 5)
foglio_calc.Cells(k, 2) = foglio_temp.Cells(i, 7)
i = i - 1
k = k + 1
Wend
file_temp.Close (no)
Else
End If
Wend
foglio_calc.Activate
Cells.Select
Selection.Sort _
Key1:=Range("A2"), Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
lRow = 2
fine = foglio_calc.Range("a1048576").End(xlUp).Row
Do While (Cells(lRow, 1) <> "") < fine
ItemRow1 = Cells(lRow, "A")
ItemRow2 = Cells(lRow + 1, "A")
If ((ItemRow1 = ItemRow2)) Then
Cells(lRow, "B") = Cells(lRow, "B") + Cells(lRow + 1, "B")
Rows(lRow + 1).Delete
Else
lRow = lRow + 1
End If
Loop
End Sub
|
Option Explicit
Sub Nomesub()
Dim wb As Workbook, wbA As Workbook, wbB As Workbook
Dim ws As Worksheet, wsA As Worksheet, wsB As Worksheet
Dim x As Long, y As Long
Dim oPvtCch As PivotCache
Dim oPvtTbl As PivotTable
Dim ptField As PivotField
Dim ptItem As PivotItem
Dim rngDati As Range
'DA MODIFICARE SUI NOMI DEI FILE E PREVEDERE DI APRIRLI, ORA PREVEDE DI AVERE LE 3 CARTELLE APERTE CONTEMPORANEAMENTE
Set wb = ThisWorkbook
Set ws = wb.Worksheets.Add
Set wbA = Application.Workbooks("fileA.xlsx")
Set wsA = wbA.Worksheets(1)
Set wbB = Application.Workbooks("fileB.xlsx")
Set wsB = wbB.Worksheets(1)
'COPIO COLONNE CELLE A:B DI FILE A NEL FILE FINALE
x = wsA.Range("A" & wsA.Rows.Count).End(xlUp).Row
wsA.Range("A1:B" & x).Copy ws.Cells(1, 1)
'COPIO COLONNE CELLE A:B DI FILE B NEL FILE FINALE
y = wsB.Range("A" & wsB.Rows.Count).End(xlUp).Row
wsB.Range("A2:B" & x).Copy ws.Cells(x + 1, 1)
'PIVOTTIZZO I DATI ACCODATI
x = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set rngDati = ws.Range("a1:b" & x)
Set oPvtCch = ws.Parent.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngDati)
Set oPvtTbl = oPvtCch.CreatePivotTable(ws.Cells(2, 5))
With oPvtTbl
.RowAxisLayout xlOutlineRow
.Name = ws.Name
.PivotFields(1).Orientation = xlRowField
.PivotFields(1).Position = 1
.AddDataField .PivotFields(2), "Totale", xlSum
End With
Set ws = Nothing
Set wb = Nothing
Set wbA = Nothing
Set wsA = Nothing
Set wbB = Nothing
Set wsB = Nothing
Set rngDati = Nothing
Set oPvtCch = Nothing
Set oPvtTbl = Nothing
End Sub
|
'COPIO COLONNE CELLE A:B DI FILE B NEL FILE FINALE
y = wsB.Range("A" & wsB.Rows.Count).End(xlUp).Row
wsB.Range("A2:B" & y).Copy ws.Cells(x + 1, 1) |
'PIVOTTIZZO I DATI ACCODATI
x = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set rngDati = ws.Range("a1:b" & x)
rngDati.Columns(1).Replace What:=" ", Replacement:="" |
Set oPvtCch = foglio_calc.Parent.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngDati) |
Option Explicit
Sub Nomesub()
Dim wb As Workbook, wbA As Workbook, wbB As Workbook
Dim ws As Worksheet, wsA As Worksheet, wsB As Worksheet
Dim x As Long, y As Long
Dim oPvtCch As PivotCache
Dim oPvtTbl As PivotTable
Dim ptField As PivotField
Dim ptItem As PivotItem
Dim rngDati As Range
'DA MODIFICARE SUI NOMI DEI FILE E PREVEDERE DI APRIRLI, ORA PREVEDE DI AVERE LE 3 CARTELLE APERTE CONTEMPORANEAMENTE
Set wb = ThisWorkbook
Set ws = wb.Worksheets.Add
Set wbA = Application.Workbooks("fileA.xlsx")
Set wsA = wbA.Worksheets(1)
Set wbB = Application.Workbooks("fileB.xlsx")
Set wsB = wbB.Worksheets(1)
'COPIO COLONNE CELLE A:B DI FILE A NEL FILE FINALE
x = wsA.Range("A" & wsA.Rows.Count).End(xlUp).Row
wsA.Range("A1:B" & x).Copy ws.Cells(1, 1)
'COPIO COLONNE CELLE A:B DI FILE B NEL FILE FINALE
y = wsB.Range("A" & wsB.Rows.Count).End(xlUp).Row
wsB.Range("A2:B" & x).Copy ws.Cells(x + 1, 1)
'PIVOTTIZZO I DATI ACCODATI
x = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set rngDati = ws.Range("a1:b" & x)
rngDati.Columns(1).Replace What:=" ", Replacement:=""
Set oPvtCch = ws.Parent.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngDati)
Set oPvtTbl = oPvtCch.CreatePivotTable(ws.Cells(2, 5))
With oPvtTbl
.RowAxisLayout xlOutlineRow
.Name = ws.Name
.PivotFields(1).Orientation = xlRowField
.PivotFields(1).Position = 1
.AddDataField .PivotFields(2), "Totale", xlSum
End With
'PROVIAMO CON MSQUERY
With ws.ListObjects.Add(SourceType:=0, Source:="ODBC;DSN=Excel Files;DBQ=" & wb.FullName & ";DefaultDir=" & wb.Path & ";DriverId=1046;MaxBufferSize=2048;PageTimeout=5;" _
, Destination:=ws.Cells(2, 8)).QueryTable
.CommandText = Array("SELECT `" & ws.Name & "$`.NOMI, Sum(`" & ws.Name & "$`.VALORI) AS 'VALORI'" & Chr(13) & "" & Chr(10) & _
"FROM `" & ws.Name & "$` `" & ws.Name & "$`" & Chr(13) & "" & Chr(10) & "GROUP BY `" & ws.Name & "$`.NOMI")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = ws.Name
.Refresh BackgroundQuery:=False
End With
Set ws = Nothing
Set wb = Nothing
Set wbA = Nothing
Set wsA = Nothing
Set wbB = Nothing
Set wsB = Nothing
Set rngDati = Nothing
Set oPvtCch = Nothing
Set oPvtTbl = Nothing
End Sub
|
'PROVIAMO CON MSQUERY
With ws.ListObjects.Add(SourceType:=0, Source:="ODBC;DSN=Excel Files;DBQ=" & wb.FullName & _
";DefaultDir=" & wb.Path & ";DriverId=1046;MaxBufferSize=2048;PageTimeout=5;" _
, Destination:=ws.Cells(2, 8)).QueryTable
.CommandText = "SELECT `" & ws.Name & "$`.NOMI, Sum(`" & ws.Name & "$`.VALORI) AS VALORI" & Chr(13) _
& "" & "FROM `" & ws.Name & "$` `" & ws.Name & "$`" & Chr(13) & "" & "GROUP BY `" & ws.Name & "$`.NOMI"
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells 'messo overwrite qualora volessi usare sempre lo stesso foglio
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = ws.Name
.Refresh BackgroundQuery:=False
End With
|
Option Explicit
Sub Nomesub()
Dim wb As Workbook, wbA As Workbook, wbB As Workbook
Dim ws As Worksheet, wsA As Worksheet, wsB As Worksheet
Dim x As Long, y As Long
Dim oPvtCch As PivotCache
Dim oPvtTbl As PivotTable
Dim ptField As PivotField
Dim ptItem As PivotItem
Dim rngDati As Range
'DA MODIFICARE SUI NOMI DEI FILE E PREVEDERE DI APRIRLI, ORA PREVEDE DI AVERE LE 3 CARTELLE APERTE CONTEMPORANEAMENTE
Set wb = ThisWorkbook
Set ws = wb.Worksheets.Add
Set wbA = Application.Workbooks("fileA.xlsx")
Set wsA = wbA.Worksheets(1)
Set wbB = Application.Workbooks("fileB.xlsx")
Set wsB = wbB.Worksheets(1)
'COPIO COLONNE CELLE A:B DI FILE A NEL FILE FINALE
x = wsA.Range("A" & wsA.Rows.Count).End(xlUp).Row
wsA.Range("A1:B" & x).Copy ws.Cells(1, 1)
'COPIO COLONNE CELLE A:B DI FILE B NEL FILE FINALE
y = wsB.Range("A" & wsB.Rows.Count).End(xlUp).Row
wsB.Range("A2:B" & y).Copy ws.Cells(x + 1, 1)
'PIVOTTIZZO I DATI ACCODATI
x = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set rngDati = ws.Range("a1:b" & x)
rngDati.Columns(1).Replace What:=" ", Replacement:=""
Set oPvtCch = ws.Parent.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngDati.Address)
Set oPvtTbl = oPvtCch.CreatePivotTable(ws.Cells(2, 5))
With oPvtTbl
.RowAxisLayout xlOutlineRow
.Name = ws.Name
.PivotFields(1).Orientation = xlRowField
.PivotFields(1).Position = 1
.AddDataField .PivotFields(2), "Totale", xlSum
End With
'PROVIAMO CON MSQUERY
With ws.ListObjects.Add(SourceType:=0, Source:="ODBC;DSN=Excel Files;DBQ=" & wb.FullName & _
";DefaultDir=" & wb.Path & ";DriverId=1046;MaxBufferSize=2048;PageTimeout=5;" _
, Destination:=ws.Cells(2, 8)).QueryTable
.CommandText = "SELECT `" & ws.Name & "$`.NOMI, Sum(`" & ws.Name & "$`.VALORI) AS VALORI" & Chr(13) _
& "" & "FROM `" & ws.Name & "$` `" & ws.Name & "$`" & Chr(13) & "" & "GROUP BY `" & ws.Name & "$`.NOMI"
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells 'messo overwrite qualora volessi usare sempre lo stesso foglio
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = ws.Name
.Refresh BackgroundQuery:=False
End With
Set ws = Nothing
Set wb = Nothing
Set wbA = Nothing
Set wsA = Nothing
Set wbB = Nothing
Set wsB = Nothing
Set rngDati = Nothing
Set oPvtCch = Nothing
Set oPvtTbl = Nothing
End Sub
|
