Re: (Macro VBA)ต้องการดึงไฟล์มาทีละหลายๆไฟล์ นำมาแปลงไฟล์ครับ
Posted: Fri Sep 22, 2017 10:24 pm
Code: Select all
Sub Macro1()
' Macro1 Macro
Dim strPath As Variant, i As Integer
Dim fName As String, wb As Workbook, tb As Workbook
Set tb = ThisWorkbook
strPath = Application.GetOpenFilename("Text files(*.txt*),*.txt*", _
Title:="Please select text files.", MultiSelect:=True)
If TypeName(strPath) = "Boolean" Then Exit Sub
For i = 1 To UBound(strPath)
fName = VBA.Left(strPath(i), InStr(strPath(i), ".")) & "xlsx"
Workbooks.OpenText Filename:=strPath(i), Origin:=874, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, Comma:=False, _
Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3 _
, 1), Array(4, 2), Array(5, 9), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, _
1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array(16, 1), _
Array(17, 1), Array(18, 1), Array(19, 1), Array(20, 1), Array(21, 3), Array(22, 4)), _
TrailingMinusNumbers:=True
Next i
For Each wb In Workbooks
If InStr(tb.Name & "incentive_A.txt" & "incentive_B.txt", wb.Name) = 0 Then
With Workbooks("incentive_A.txt").Sheets(1)
wb.Sheets(1).UsedRange.Offset(1, 0).Copy .Range("a" & .Rows.Count).End(xlUp).Offset(1, 0)
End With
With Workbooks("incentive_B.txt").Sheets(1)
wb.Sheets(1).UsedRange.Offset(1, 0).Copy .Range("a" & .Rows.Count).End(xlUp).Offset(1, 0)
End With
wb.Close False
End If
Next wb
With Workbooks("incentive_A.txt")
.Sheets(1).Name = "incentive"
.SaveAs Filename:=VBA.Left(.Name, InStrRev(.Name, ".") - 1), FileFormat:=xlOpenXMLWorkbook
.Close False
End With
With Workbooks("incentive_B.txt")
.Sheets(1).Name = "incentive"
.SaveAs Filename:=VBA.Left(.Name, InStrRev(.Name, ".") - 1), FileFormat:=xlOpenXMLWorkbook
.Close False
End With
MsgBox "Finish."
End Sub