Tree_Chee
Tree_Chee

Reputation: 31

How to close all the excel file open

I was running Marco to open all the .txt file in the list at ChDir. And append data from the .txt to the Master file .xls. But when run the script , the .txt file will be open with .xls in the PC . If there are 5 .txt file , it will open 5 .xls file. My question is how can i close all the 5 .xls file once script have append needed data into it? I do not want to close it manually because the input file might be a lot in future. Thanks.

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

Upvotes: 0

Views: 1067

Answers (2)

user4039065
user4039065

Reputation:

After opening the TXT file and copying the information to the Composite workbook, you should be able to close each one in sequence as you use it.

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

I did not set up a full test environment to this but is cycled through the three TXT I created with limited fields.

Upvotes: 0

mk117
mk117

Reputation: 775

Try this:

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

(source)

EDIT: Since you're appending data to your 5 xls files, then I assume you need to save these workbooks before closing. @Nathan_Sav: Code edited!

Upvotes: 1

Related Questions