
Sub estrai()
'
' Macro4 Macro
'
ChDir "C:UsersstefanoDesktopluglio_13"
myFile = Application.GetOpenFilename("text Files,*.asc")
Workbooks.OpenText _
Filename:=myFile, _
Origin:=xlMSDOS, _
StartRow:=1, _
DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array( _
8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(20, 1), Array(21, 1), Array(27, 1), _
Array(28, 1), Array(36, 1), Array(37, 1), Array(42, 1), Array(43, 1), Array(78, 1), Array( _
79, 1), Array(114, 1), Array(115, 1), Array(120, 1), Array(148, 1), Array(150, 1), Array( _
151, 1)), TrailingMinusNumbers:=True
ActiveSheet.Move Before:=Workbooks("esporta_rimborsi.xlsm").Sheets(1)
Range("B:B,D:D,F:F,H:H,J:J,L:L,N:N,P:P,T:T").Select
Range("T1").Activate
Selection.Delete Shift:=xlToLeft
Columns("A:L").Select
Columns("A:L").EntireColumn.AutoFit
Range("A1").Select
End Sub |
Sub Macro1()
Dim strFile As String, strPath As String
strFile = "file_prova.ASC"
strPath = "D:DocumentsDavideProve VBA"
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & strPath & strFile, Destination:=Range("$A$1"))
.Name = "file_prova"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
La tua importando il primo campo come testo:
Sub estrai()
' Macro4 Macro
'
ChDir "D:DocumentsDavideProve VBA"
myfile = Application.GetOpenFilename("text Files,*.asc")
Workbooks.OpenText _
Filename:=myfile, _
Origin:=xlMSDOS, _
StartRow:=1, _
DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 2), Array( _
8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(20, 1), Array(21, 1), Array(27, 1), _
Array(28, 1), Array(36, 1), Array(37, 1), Array(42, 1), Array(43, 1), Array(78, 1), Array( _
79, 1), Array(114, 1), Array(115, 1), Array(120, 1), Array(148, 1), Array(150, 1), Array( _
151, 1)), TrailingMinusNumbers:=True
' ActiveSheet.Move Before:=Workbooks("esporta_rimborsi.xlsm").Sheets(1)
Range("B:B,D:D,F:F,H:H,J:J,L:L,N:N,P:P,T:T").Delete Shift:=xlToLeft
Columns("A:L").EntireColumn.AutoFit
Range("A1").Select
End Sub
|
Sub Macro1()
Dim strFile As String, strPath As String
strFile = "file_prova.ASC"
strPath = "C:UsersstefanoDesktopluglio_13"
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & strPath & strFile, Destination:=Range("$A$1"))
.Name = "file_prova"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub |
CAMBIA QUESTA RIGA FieldInfo:=Array(Array(0, 1), Array( _ IN QUESTA FieldInfo:=Array(Array(0, 2), Array( _ p.s. 1 = Generale, 2 = testo, 9 = non importare |
Option Explicit
Sub Sfoglia_Files()
Dim strPath As String
Dim FD As FileDialog
Dim objFd As Variant
Dim wb As Workbook
Dim ws As Worksheet
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
'.InitialFileName = "C:UsersstefanoDesktop" 'QUI PUOI METTERE IL PERCORSO INIZIALE, togli l'apice davanti alla riga se lo vuoi usare
.Title = "Sfoglia cartelle"
.ButtonName = "Ok"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewDetails
.Show
For Each objFd In .SelectedItems
strPath = objFd
Next objFd
End With
If strPath = "" Then GoTo Uscita
Set wb = Application.Workbooks.Add
Set ws = wb.Sheets(1)
Call Query_ASC(strPath, ws)
'qui inserire il codice per formattar ei CAP, inserire le intesazioni di colonna eccetera ;)
Uscita:
Set FD = Nothing
Set wb = Nothing
Set ws = Nothing
End Sub
Private Sub Query_ASC(ByVal strPath As String, ws As Worksheet)
With ws.QueryTables.Add(Connection:="TEXT;" & strPath, Destination:=ws.Cells(1, 1)) 'mettere cells(2,1) se poi vuoi mettere le intestazioni di colonna
.Name = "Query"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 2, 9, 1, 1, 1, 9, _
1)
.TextFileFixedColumnWidths = Array(8, 1, 1, 1, 9, 1, 6, 1, 8, 1, 5, 1, 35, 1, 3, 32, 1, 5, 28 _
, 2, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ws.Cells.EntireColumn.AutoFit
End Sub
|
Sub Sfoglia_Files()
Dim strPath As String
Dim FD As FileDialog
Dim objFd As Variant
Dim wb As Workbook
Dim ws As Worksheet
Dim x As String
Dim p As String
Dim LastRow As Long
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
'.InitialFileName = "C:UsersstefanoDesktop" 'QUI PUOI METTERE IL PERCORSO INIZIALE, togli l'apice davanti alla riga se lo vuoi usare
.Title = "Sfoglia cartelle"
.ButtonName = "Ok"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewDetails
.Show
For Each objFd In .SelectedItems
strPath = objFd
Next objFd
End With
If strPath = "" Then GoTo Uscita
Set wb = Application.Workbooks.Add
Set ws = wb.Sheets(1)
Call Query_ASC(strPath, ws)
LastRow = [counta(A:A)]
With Range("C1:C" & LastRow)
x = .Address
.Offset(, 0) = Evaluate("(" & x & ")/100")
End With
With Range("M1:M" & LastRow)
p = .Address
.Offset(, 0) = Evaluate("(" & p & ")/100")
End With
'qui inserire il codice per formattar ei CAP, inserire le intesazioni di colonna eccetera ;)
Uscita:
Set FD = Nothing
Set wb = Nothing
Set ws = Nothing
End Sub
Private Sub Query_ASC(ByVal strPath As String, ws As Worksheet)
With ws.QueryTables.Add(Connection:="TEXT;" & strPath, Destination:=ws.Cells(1, 1)) 'mettere cells(2,1) se poi vuoi mettere le intestazioni di colonna
.Name = "Query"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 2, 9, 1, 1, 1, 9, _
1)
.TextFileFixedColumnWidths = Array(8, 1, 1, 1, 9, 1, 6, 1, 8, 1, 5, 1, 35, 1, 3, 32, 1, 5, 28 _
, 2, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ws.Cells.EntireColumn.AutoFit
ActiveSheet.Move Before:=Workbooks("Cartel1").Sheets(1)
End Sub |
CAMBIA:
Set wb = Application.Workbooks.Add
Set ws = wb.Sheets(1)
IN
Set wb = thisworkbook
Set ws = wb.Sheets.add
CAMBIA POI
ws.Cells.EntireColumn.AutoFit
ActiveSheet.Move Before:=Workbooks("Cartel1").Sheets(1)
IN
ws.Cells.EntireColumn.AutoFit
ws.move Before:=ws.parent.Sheets(1)
WS.name = format(ws.cells(1,1),"mmmm") |
Private Sub Query_ASC(ByVal strPath As String, ws As Worksheet)
With ws.QueryTables.Add(Connection:="TEXT;" & strPath, Destination:=ws.Cells(1, 1)) 'mettere cells(2,1) se poi vuoi mettere le intestazioni di colonna
.Name = "Query"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 2, 9, 1, 1, 1, 9, _
1)
.TextFileFixedColumnWidths = Array(8, 1, 1, 1, 9, 1, 6, 1, 8, 1, 5, 1, 35, 1, 35, 1, 5, 28 _
, 2, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
With ws
.Cells.EntireColumn.AutoFit
.Name = Format(ws.Cells(1, 1), "mmmm yyyy")
.Move .Parent.Sheets(1)
End With
End Sub |
Private Sub Query_ASC(ByVal strPath As String, ws As Worksheet)
With ws.QueryTables.Add(Connection:="TEXT;" & strPath, Destination:=ws.Cells(1, 1)) 'mettere cells(2,1) se poi vuoi mettere le intestazioni di colonna
.Name = "Query"
.RefreshStyle = xlOverwriteCells
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileTabDelimiter = True
.TextFileColumnDataTypes = Array(1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 2, 9, 1, 1, 1, 9, 1)
.TextFileFixedColumnWidths = Array(8, 1, 1, 1, 9, 1, 6, 1, 8, 1, 5, 1, 35, 1, 35, 1, 5, 28, 2, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
With ws
.Cells.EntireColumn.AutoFit
.Name = Format(ws.Cells(1, 1), "mmmm yyyy")
.Move .Parent.Sheets(1)
End With
End Sub
|
