
Sub estrazione ()
'-----------------------ribattezzo i fogli'------------------------------------------------------------
Sheets("Foglio2").Select
Sheets("Foglio2").Name = "istogrammi"
Sheets("Foglio1").Select
Sheets("Foglio1").Name = "statistiche"
'-----------------------apertura del fileDialog che ci chiede di inserire il file----------------------
Dim inputFile As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogOpen)
dlgOpen.Filters.Clear
With dlgOpen.Filters.Add("File di testo", "*.txt", 1) 'nel fileDialog viene scritto"File di Testo" e ci si aspetta che sia un ".txt"
End With
If dlgOpen.Show <> -1 Then
MsgBox "Non hai selezionato nessun file", vbInformation 'questo è un controllo per essere sicuri che si sia inserito un file
Else
'-----------------------inizia l'importazione sempre dalla casella A1-----------------------------------
Sheets("istogrammi").Select
inputFile = dlgOpen.SelectedItems(1)
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & inputFile, Destination _
:=Range("$A$1"))
.Name = inputFile
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If
'----------------------trattamento dati-----------------------------------------
' sostituisce i punti con le virgole
Cells.Replace What:=".", Replacement:=",", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
' eliminazione delle colonne inutili
Columns("J:K").Select
Selection.Delete Shift:=xlToLeft
Columns("A:H").Select
Selection.Delete Shift:=xlToLeft
' cancella il contenuto di A1
Cells(1, 1).Select
Selection.ClearContents
' elimina le righe vuote in alto
Do
If (IsEmpty(Cells(1, 1))) Then
Cells(1, 1).EntireRow.Delete
End If
Loop While IsEmpty(Cells(1, 1))
' sposta colonna A in B
Columns("A:A").Select
Selection.Cut Destination:=Columns("B:B")
' calcola l'ultima riga
Dim ultimariga As Integer
ultimariga = Cells(Rows.Count, 2).End(xlUp).Row
' converte sigma da testo anumero
Range("C2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]*1" 'converte in numeri moltiplicando per 1
Selection.AutoFill Destination:=Range(Cells(2, 3), Cells(ultimariga, 3)), Type:=xlFillDefault
Range(Cells(2, 3), Cells(ultimariga, 3)).Select
Selection.Copy 'copi e incollo in valori
Range("D2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("B:C").Select 'tolgo le colonne in eccesso
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
'etc
End sub |
Option Explicit
Sub estrazione()
'-----------------------ribattezzo i fogli'------------------------------------------------------------
Sheets("Foglio2").Select
Sheets("Foglio2").Name = "istogrammi"
Sheets("Foglio1").Select
Sheets("Foglio1").Name = "statistiche"
'-----------------------apertura del fileDialog che ci chiede di inserire il file----------------------
Dim inputFile As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogOpen)
dlgOpen.Filters.Clear
With dlgOpen.Filters.Add("File di testo", "*.txt", 1) 'nel fileDialog viene scritto"File di Testo" e ci si aspetta che sia un ".txt"
End With
If dlgOpen.Show <> -1 Then
MsgBox "Non hai selezionato nessun file", vbInformation 'questo è un controllo per essere sicuri che si sia inserito un file
Else
'-----------------------inizia l'importazione sempre dalla casella A1-----------------------------------
Sheets("istogrammi").Select
inputFile = dlgOpen.SelectedItems(1)
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & inputFile, Destination:=Range("$A$1"))
.Name = inputFile
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If
'----------------------trattamento dati-----------------------------------------
' sostituisce i punti con le virgole
Cells.Replace What:=".", Replacement:=",", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
' eliminazione delle colonne inutili
Columns("J:K").Delete
Columns("A:H").Delete
' cancella il contenuto di A1
Cells(1, 1).ClearContents
' elimina le righe vuote in alto
Do
If (IsEmpty(Cells(1, 1))) Then
Cells(1, 1).EntireRow.Delete
End If
Loop While IsEmpty(Cells(1, 1))
' sposta colonna A in B
Columns("A:A").Select
Selection.Cut Destination:=Columns("B:B")
' calcola l'ultima riga
Dim ultimariga As Long
ultimariga = Cells(Rows.Count, 2).End(xlUp).Row
Range("D2:D" & ultimariga) = Evaluate(Range("B2:B" & ultimariga).Address & "*1")
Columns("B:C").Delete
'etc
End Sub
|
