如何关闭所有excel文件打开

时间:2016-02-18 07:45:41

标签: excel vba

我正在运行Marco打开ChDir列表中的所有.txt文件。 并将.txt中的数据附加到主文件.xls。 但是当运行脚本时,.txt文件将在PC中以.xls打开。 如果有5个.txt文件,则会打开5个.xls文件。 我的问题是,一旦脚本将所需的数据附加到其中,我如何关闭所有5个.xls文件? 我不想手动关闭它,因为将来输入文件可能会很多。谢谢。

Sub Macro1()

For r = 2 To 400
c = 1

If (Sheets("File").Cells(r, c)) = "" Then
r = 401

Else

ChDir "C:\Users\csleow\Desktop\wafermap_stack"


Workbooks.OpenText Filename:= _
    Sheets("File").Cells(r, c) _
    , Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
    Array(0, 1), Array(5, 1), Array(10, 1), Array(15, 1), Array(20, 1), Array(25, 1), Array(30, _
    1), Array(35, 1), Array(40, 1), Array(45, 1), Array(50, 1), Array(55, 1), Array(60, 1), _
    Array(65, 1), Array(70, 1), Array(75, 1), Array(80, 1), Array(85, 1), Array(90, 1), Array( _
    94, 1), Array(99, 1), Array(104, 1), Array(109, 1), Array(114, 1), Array(119, 1), Array(124 _
    , 1), Array(129, 1), Array(134, 1), Array(139, 1), Array(144, 1), Array(149, 1), Array(154, _
    1), Array(159, 1), Array(164, 1), Array(169, 1), Array(174, 1), Array(179, 1), Array(184, 1 _
    ), Array(189, 1), Array(194, 1), Array(199, 1), Array(204, 1), Array(209, 1), Array(214, 1) _
    , Array(219, 1), Array(224, 1), Array(229, 1), Array(234, 1), Array(239, 1), Array(244, 1), _
    Array(249, 1), Array(254, 1), Array(259, 1), Array(264, 1), Array(269, 1), Array(274, 1), _
    Array(279, 1), Array(284, 1), Array(289, 1), Array(294, 1), Array(299, 1), Array(304, 1), _
    Array(309, 1), Array(315, 1), Array(320, 1), Array(325, 1), Array(330, 1), Array(335, 1), _
    Array(340, 1), Array(345, 1), Array(350, 1), Array(355, 1), Array(360, 1)), _
    TrailingMinusNumbers:=True

Cells.Select
Selection.Copy
Windows("Composite_9221.xlsm").Activate
Sheets("Temp").Select
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select

Macro3

End If

Next

End Sub

2 个答案:

答案 0 :(得分:1)

试试这个:

For Each WkbkName In Application.Workbooks()
  If WkbkName.Name <> ThisWorkbook.Name Then WkbkName.Close SaveChanges:=True
Next

source

编辑:由于您要将数据附加到5 xls文件,因此我假设您需要在关闭之前保存这些工作簿。 @Nathan_Sav:代码已编辑!

答案 1 :(得分:0)

打开TXT文件并将信息复制到Composite工作簿后,您应该能够在使用它时按顺序关闭每个文件。

Sub Macro1()
    Dim r As Long, c As Long, fp As String

    fp = "C:\Users\csleow\Desktop\wafermap_stack" & Chr(92)

    With Worksheets("file")
        For r = 2 To 400
            c = 1

            If .Cells(r, c) = "" Then
                r = 401
            Else
                With .Parent.OpenText(Filename:=fp & Cells(r, c), _
                        Origin:=437, StartRow:=1, DataType:=xlFixedWidth, _
                        FieldInfo:=Array(Array(0, 1), Array(5, 1), Array(10, 1), Array(15, 1), _
                                         Array(20, 1), Array(25, 1), Array(30, 1), Array(35, 1), _
                                         Array(40, 1), Array(45, 1), Array(50, 1), Array(55, 1), _
                                         Array(60, 1), Array(65, 1), Array(70, 1), Array(75, 1), _
                                         Array(80, 1), Array(85, 1), Array(90, 1), Array(94, 1), _
                                         Array(99, 1), Array(104, 1), Array(109, 1), Array(114, 1), _
                                         Array(119, 1), Array(124, 1), Array(129, 1), Array(134, 1), _
                                         Array(139, 1), Array(144, 1), Array(149, 1), Array(154, 1), _
                                         Array(159, 1), Array(164, 1), Array(169, 1), Array(174, 1), _
                                         Array(179, 1), Array(184, 1), Array(189, 1), Array(194, 1), _
                                         Array(199, 1), Array(204, 1), Array(209, 1), Array(214, 1), _
                                         Array(219, 1), Array(224, 1), Array(229, 1), Array(234, 1), _
                                         Array(239, 1), Array(244, 1), Array(249, 1), Array(254, 1), _
                                         Array(259, 1), Array(264, 1), Array(269, 1), Array(274, 1), _
                                         Array(279, 1), Array(284, 1), Array(289, 1), Array(294, 1), _
                                         Array(299, 1), Array(304, 1), Array(309, 1), Array(315, 1), _
                                         Array(320, 1), Array(325, 1), Array(330, 1), Array(335, 1), _
                                         Array(340, 1), Array(345, 1), Array(350, 1), Array(355, 1), _
                                         Array(360, 1)), _
                        TrailingMinusNumbers:=True)
                    .Worksheets(1).Cells(1, 1).CurrentRegion.Cells.Copy _
                      Destination:=Workbooks("Composite_9221.xlsm").Worksheets("Temp").Range("A1")
                    .Close savechanges:=False
                End With
                'done with the the TXT fle, close it

                'I don't know what this does
                Macro3

            End If

        Next r
    End With

End Sub

我没有为此设置完整的测试环境,而是通过我用有限字段创建的三个TXT循环。