Dùng code để import text vào excel cùng lúc nhiều file (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

Lê Duy Thương

Cạo lấy gì gội (Dịch quá không gội được)
Tham gia
14/10/09
Bài viết
3,112
Được thích
4,848
hiện tôi đang gặp khó khăn . mong các sư phụ, các anh, các chị giúp đỡ
hiện tôi đang có 36 file text có cấu trúc bên trong giống nhau. tên file là " 2009_12" đến "2012_12".dùng công cụ import text vào excel thì tôi đã biết.nhưng phải làm thủ công 36 lần mỏi hết cả tay.vậy có code nào để import vào excel 1 lần được 36 file không ?
rất mong các sư phụ, các anh chị trợ giúp
trân trọng
ldt
 

File đính kèm

Tôi có ghi 1 macro, bác xem thử có dùng được không:
(dữ liệu sẽ ghi vào sheet2)
PHP:
Sub ImportText()
Dim FD As FileDialog
Dim FFs As FileDialogFilters
Dim strFileName, raKQ As Range
On Error GoTo Problem
Set FD = Application.FileDialog(msoFileDialogOpen)
    With FD
        Set FFs = .Filters
        With FFs
        .Clear
        .Add "TXT", "*.txt"
        End With
        .AllowMultiSelect = True
    If .Show = False Then Exit Sub
    Set raKQ = Sheet2.Range("A1")
    raKQ.CurrentRegion.ClearContents
    For Each strFileName In .SelectedItems
        'raKQ.Offset(0, 10) = strFileName - chi de kiem tra'
        Call Import1(strFileName, raKQ)
        Set raKQ = raKQ.Cells(1).End(xlDown).Offset(1, 0)
       
    Next
    End With
    Exit Sub
Problem:
MsgBox "You have not selected a valid txt-file."
End Sub
Sub Import1(fName, ra As Range)
    With ActiveSheet.QueryTables.Add( _
        Connection:="TEXT;" & fName, _
        Destination:=ra)
        .Name = "2011_01"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 1252
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
   
End Sub
 
Em xin phép được gửi bài vào topic này luôn.
Em muốn nhờ các Thầy và các anh chị trên diễn đàn viết code giúp em để import dữ liệu từ file txt sang excel có lọc theo các điều kiện em đã ghi sẵn trong file excel. Em chỉ muốn lấy hai cột ở trong file text vào excel, cột số thiết bị và cột số máy.
Trân trọng cảm ơn các Thầy và các anh chị.

Trần Văn Thành.
 
Lần chỉnh sửa cuối:
Web KT

Bài viết mới nhất

Back
Top Bottom