按照修改日期的顺序从文件中将数据输入到工作表中

时间:2015-06-08 14:11:21

标签: excel-vba vba excel

现在我有一个宏从.chr文件中检索数据,该文件与excel工作簿位于同一文件夹中。它遍历文件并按文件名顺序放入工作表中。我想将数据按修改后的数据顺序排列。如果有人可以使用filedatetime函数向我展示一些示例代码,或者w.e,那将是很好的

 Dim myName, myPath, myName1, qt1
Dim counter, counter1 As Integer
Dim OK As Integer
Dim I As Integer

myPath = ThisWorkbook.Path & "\"
myName = Dir(myPath & "*.txt")
Worksheets(1).Activate
Worksheets.Add
Worksheets(1).Name = "merge__chr"
Cells.Select
Selection.Delete Shift:=xlUp
Range("A1").Select
counter = 1
counter1 = 1
OK = 0
OK = InStr(1, myName, "chr.txt", vbTextCompare)
Do While myName <> ""

    Do While OK = 0 And myName <> ""
        myName = Dir
        OK = InStr(1, myName, "chr.txt", vbTextCompare)
    Loop

    myName1 = "TEXT;" & myPath & myName
    If OK <> 0 Then
        With Worksheets("merge__chr").QueryTables.Add(Connection:=myName1, Destination:=Worksheets("merge__chr").Cells(counter1, 1))
            If counter1 > 1 Then
                .TextFileStartRow = 2
            End If
            .Refresh
        End With
        For I = 1 To 2
        If Worksheets("merge__chr").Cells(counter1, 1) = "" Then
            Set currentCell = Worksheets("merge__chr").Cells(counter1, 1)
            currentCell.EntireRow.Delete
        End If
        counter1 = counter1 + 1
        Next
        counter1 = 1
        Do While Worksheets("merge__chr").Cells(counter1, 1) <> ""
            counter1 = counter1 + 1
        Loop
    End If
    OK = 0

Loop
Do While Worksheets("merge__chr").Cells(counter, 1) <> ""
    If Worksheets("merge__chr").Cells(counter, 1) = "END" Then
        Set currentCell = Worksheets("merge__chr").Cells(counter, 1)
        currentCell.EntireRow.Delete
    End If
    counter = counter + 1
Loop

0 个答案:

没有答案