Online method with multiple files looking for EXCEL, ideas:

The Internet to find EXCEL file with multiple methods, ideas:

A, Linux or window + cmder, directly from the command line cat merge EXCEL file, however, you need to install a secondary thing to deal directly (and perhaps can not, however, convert a file format conversion tool is feasible to convert the file into EXCEL txt file, re-operation, good operation, converted to EXCEL format); there is a perl or Python has its own built-in EXCEL processing package, can be like handling arrays, direct purpose of processing EXCEL file

Two, EXCEL VBA programming comes with the merger, however, a lot of online search, the format is very similar to the VBA script, can not achieve perfect merge functions are more or less associated with incomplete phenomenon, then encountered the following this version can be well implemented correctly merge, in this script inside each of the functions to the detailed notes about, if not the right place, welcome message, grateful ~

script:

Sub directory merge all workbooks All Sheets ()  

Dim MP, MN, AW, Wbn, wn # define the variable (MP = MyPath, MN = MyName, AW = ActiveWorkbookName, Wbn = WorkBookName, wn = workbooksheet (i) name), but did not specify the type of variable, this specification is not very

Dim Wb As Workbook # variable is defined for the workbook Wb type #Dim Wbn As string, G As Long # is defined as a character variable Wbn, G is a long integer #Dim Num, ini As Long # define Num unspecified types, defined and ini statement to a long integer

Dim i, a, b, d, c, e # define variables, without specifying the type of variable, this specification is not very

Application.ScreenUpdating = False # turn off screen refresh

MP = ActiveWorkbook.Path # the current workbook (the active workbook) path assigned to MP

MN = Dir (MP & "\ " & "* .xls") # the current workbook (the active workbook) path plus \ *. Xls suffix, so the value of all the file names of the captured * position, Dir  return match  pathname  of a file name. If you want to get other matching  pathname  of the file name, and once again call  Dir , and do not use parameters. If the file is not in line with the conditions, the  Dir  returns a zero-length string ( ""). Once the return value is zero-length string, and to be called again  Dir  time, you must specify  pathname , otherwise it will generate an error.

AW = ActiveWorkbook.Name # assign the current workbook (the active workbook) name to the AW (without suffix, only the name)

Num num = 0 = 0 #

The e = 1 # = 0

Do While MN <> "" # DO while running the following cycles, until the MN value is null

  If MN <> AW Then # if, MN AW value is not equal to the value, runs between IF END IF statement determination

    Set Wb = Workbooks.Open (MP & "\" & MN) # Open MP \ path under a workbook called MN variable values, and references (Set of role) is assigned Wb ## references assignment if, for a change Wb , was also cited as changed, for details see below PS (3)

    a = a + 1 # accumulate on a circulating

    With Workbooks (1) .ActiveSheet # application with all the statements that have been open workbook first workbook is active worksheet  

      For i = 1 To Sheets.Count # all sheet Workbooks (1) .ActiveSheet circulating

        If Sheets (i) .Range ( "a1") <> "" Then # A1 cell contents if the workbook Wb i-th sheet is not empty, proceeds IF content determination, if it is empty, skip IF For determination of the next cycle

          Wb.Sheets (i) .Range ( "a1"). Resize (1, Sheets (i) .UsedRange.Columns.Count) .Copy .Cells (1, 1) # wb workbook to the i-th sheet A1 expanded to cell range line with signs of application columns (x) the size of the region, i.e., A1: x1 region, copying the contents of the expanded region after the Workbooks (1) .ActiveSheet position A1 

          d = Wb.Sheets (i) .UsedRange.Columns.Count #wb workbook sheet has the i-th column count traces application, and assigned to d

          c = Wb.Sheets (i) .UsedRange.Rows.Count - 1 #wb workbook sheet has the i-th row count traces application, and assigned to c

          Name wn = Wb.Sheets (i) .Name #wb the i-th workbook worksheet assigned to wn

          .Cells (1, d + 1) = " table" #Workbooks (1) .ActiveSheet first row of the worksheet, the d + 1 column of cells filled with "table" string 
          .Cells (e + 1, d + 1) .Resize (c, 1 ) = MN & wn #Workbooks (1) .ActiveSheet e + 1 the first row of the worksheet, the first expansion column region d + 1 row is c, an area, and filling in the region is & Wn of the MN
          E = E + C                   
          Wb.Sheets (I) .Range ( "A2"). the Resize (C, D) .Copy .Cells (.Range ( "a1048576"). End (xlUp) +. 1 .Row 1) # the content area,复制到Workbooks(1).ActiveSheet中,每次从Workbooks(1).ActiveSheet的最后一个非空行开始粘贴

        End If

      The Next
      Wbn Wbn = & Chr (13) & wb.Name # Wbn the value of the name after adding a space and Wb workbooks assigned to Wbn

      Wb.Close False # Wb workbook will close
    End With
  End the If
the MN # = Dir Dir match to get on top of the next file name; # Dir  will return a match  pathname  of a file name. If you want to get other matching  pathname  of the file name, and once again call  Dir , and do not use parameters. If the file is not in line with the conditions, the  Dir  returns a zero-length string ( ""). Once the return value is zero-length string, and to be called again  Dir  time, you must specify  pathname , otherwise it will generate an error.
Loop
. The Range ( "A1") the Select # Select current first cell workbook
Application.ScreenUpdating = True # open screen refresh
MsgBox "were merged" & a & "under all working thin sheets as follows:." & Chr (13) & Wbn, vbInformation, " prompt" # give final tips

End Sub

Reference: https: //www.jianshu.com/p/f52e6edc2d63

Reference: https:? //Zhinan.sogou.com/guide/detail/ id = 1610003487 # The module is added Button (Form Controls)

Note: The first line is the line had to be merged header, leaving only a first line and a merged document, there is no row header to be wrong, missing data; cell A1 of each file can not be merged is empty cell, otherwise the entire file will not do the merger;

 

Example:

1 files are:

2 files are:

 

 

1, 2 are combined to file, an empty file is a new 3; to put a directory folder;

3 Open the file, open the VBA, paste the script save, save the file 3 file format to enable macros;

Run the macro;

The results show:

Description: Use words directly copy the script can be pasted under the PPS, to be replaced in order to comply VBA # comment format '

 

 

PS:

(1)Resize

Resize Range object using the specified size of the area attribute adjustment, and returns the size range of cells adjusted, as shown in the code below.
The Resize Sub ()
. Sheet4.Range ( "A1") the Resize (3, 3) .Select 'means that the cells in the worksheet A1 4 is expanded to the size of the area of 3 rows and 3 columns, the result is A1: C3 region
End Sub
code analysis:
Resize process uses Range Resize object properties of the selected cell A1 expanded region after three rows and three columns.
Resize attribute syntax is as follows:
expression.Resize (RowSize, the ColumnSize)
Parameters expression is required, return the object to adjust the size of the Range
parameter RowSize is optional, the number of rows in the new region. If omitted, the number of rows in the region remains unchanged.
ColumnSize parameter is optional, the number of columns in the new region. If you omitted. The number of columns in the region remains unchanged.

Reference: http: //www.excelpx.com/thread-174857-1-1.html

(2) you

Custom function determines whether the configuration file exists in function This is a VBA the Dir ()

Function FileExists(fname) As Boolean ' Returns TRUE if the file exists Dim x As String x = Dir(fname) If x <> "" Then FileExists = True _ Else FileExists = False End Function

////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////// Dir function

Returns a  String , to represent a file name, directory name or folder name, which must match a specified pattern or file attribute, or a disk label.

grammar

Dir[(pathname[, attributes])]

Dir  grammar function has the following parts:

Part Description pathname optional parameters. It is used to specify the filename string expression, may contain directories or folders, and drives. If you do not find the  pathname , it will return a zero-length string ( ""). attributes Optional. Numeric constant or expression that is used to specify the total file attributes. If omitted, it will return a match  pathname  file but does not contain the property.

Settings

attributes  set the parameters may be:

Constants described vbNormal 0 (default) does not attribute file. vbReadOnly . 1 specifies no read-only file attribute vbHidden 2 specifies no hidden file attribute vbSystem . 4 no attribute specified in the Macintosh file system is not available. vbVolume 8 specify the volume label file; if the other attributes specified, is ignored vbVolume  the Macintosh is not available. vbDirectory 16 properties file and specify no path and folder. vbAlias 64-specified file name is an alias, available only on the Macintosh.

 

Note that  these constants are designated by the VBA, anywhere in the program code, the real value may be replaced with these constants.

Explanation

In Microsoft Windows,  Dir  supports multiple character ( *)  and single character ( ?)  Wildcards to specify multiple files.

On the Macintosh, these characters as a legitimate file name characters and not as a wildcard to specify multiple files

Since the Macintosh does not support wildcards, file types specified file group. You can use  MacID  function to specify file type instead of the file name. For example, the following statement returns the name of the first file in the current folder TEXT:

Dir("SomePath", MacID("TEXT"))

All files for the selected folder, specify an empty string:

Dir("")

In Microsoft Windows, if the Dir using the function MacID function, an error occurs.

No greater than 256 attribute values are considered MacID  value of the function.

In the first call  Dir  a function, you must specify  pathname , otherwise it will generate an error. If you also specify the file attribute, then it must include the  pathname .

Dir  will return a match  pathname  of a file name. If you want to get other matching  pathname  of the file name, and once again call  Dir , and do not use parameters. If the file is not in line with the conditions, the  Dir  returns a zero-length string ( ""). Once the return value is zero-length string, and to be called again  Dir  time, you must specify  pathname , otherwise it will generate an error. Do not have access to all match the current  pathname  of the file name, you can change to a new  pathname  on. However, it can not be called recursively  Dir  function. To  vbDirectory  property to call the  Dir  can not continually return subdirectories.

Tip  Since the file name in a special order and will not be returned, so it can be in an array, and then sort the file names are stored on the array.

Reference: http: //club.excelhome.net/thread-121668-1-1.html

(3)Set

VBA is SET in a customized object properties and methods

Excel version Reference: 2010
1, grammar

Set objectvar = {[New] 
objectexpression | Nothing}

Set statement syntax has the following parts:

description

objectvar    
Required. Name of the variable or property, follows standard variable naming conventions.    

New    
Optional. New typically used in the statement, so that you can implicitly created object. If the New and
Used with Set, a new instance of the class is created. in case
objectvar contains an object reference , that reference is released when assigning new values. You can not use the New keyword to create a new instance of any internal data types, can not create dependent objects.    

objectexpression    
Required. By the object name, other variables the same type of objects declared or returns the expression or function of the same object type method thereof.    

Nothing    
Optional. Objectvar sever associated with any of the specified object. If no other variable to point
Objectvar original objects referenced, it is assigned to Nothing releases all the system and memory resources associated with the object.    
2, Description:

In order to ensure legal, objectvar must be consistent with the object being assigned to the object type.

Dim, Private, Public, ReDim, and Static statements only declare a variable reference object. Using
Before the Set statement to assign a specific object variable, the variable does not refer to any actual object.

The following example shows how to use Dim to declare an array of type Form1. Form1 not actually instances. Then use
Set a reference to an instance of Form1 assigning the newly created myChildForms variables. In MDI
Applications can use the code to create a subform.
Dim myChildForms(1 to 4) As Form1
Set myChildForms(1) = New Form1
Set myChildForms(2) = New Form1
Set myChildForms(3) = New Form1
Set myChildForms(4) = New Form1

Typically, when you use Set to assign an object reference variables, not for variables create a copy of the object, but the object is to create a reference. There can be multiple object variables refer to the same object. Because these variables only reference to the object, rather than a copy of the object, and therefore the reaction will object to any changes to the object variable references all. However, if the
Set statement using the New keyword, it would in effect create a new instance of the object.
3, Set Statement Example

This example uses the Set statement to assign an object reference variables. Text has assumed a point YourObject
Legitimate object properties.
Dim YourObject, MyObject, MyStr
Set MyObject = YourObject 'object reference assignment.
'MyObject and YourObject reference the same object.
YourObject.Text = "Hello World" 'initialization properties.
MyStr = MyObject.Text 'returns "Hello World".
'Disassociate. MyObject no longer referenced YourObject.
Set MyObject = Nothing 'release the object.
 Reference: https: //wenwen.sogou.com/z/q705635956.htm
 
 
PPS :( copy and paste script can be, revised in line with VBA comment symbol)

Sub directory merge all workbooks All Sheets ()

Dim MP, MN, AW, Wbn, wn 'define the variable (MP = MyPath, MN = MyName, AW = ActiveWorkbookName, Wbn = WorkBookName, wn = workbooksheet (i) name), but did not specify the type of variable, this specification is not very

Dim Wb As Workbook 'is defined for the workbook type variable Wb #Dim Wbn As string, G As Long # is defined as a character variable Wbn, G is a long integer #Dim Num, ini As Long # define Num unspecified types, defined and ini statement to a long integer

Dim i, a, b, d, c, e 'define the variable, but do not specify the type of variable, this specification is not very

Application.ScreenUpdating = False 'turn off the screen refresh

MP = ActiveWorkbook.Path 'current workbook (the active workbook) path assigned to MP

MN = Dir (MP & "\" & "* .xls") 'current workbook (the active workbook) path plus \ *. Xls suffix, so the value of all the file names of the captured * position, Dir It returns the first match pathname of the file name. If you want to get other matching pathname of the file name, and once again call Dir, and do not use parameters. If the file is not in line with the conditions, the Dir returns a zero-length string ( ""). Once the return value is zero-length string, and to call Dir again, you must specify pathname, otherwise it will generate an error.

AW = ActiveWorkbook.Name 'current workbook (the active workbook) the name assigned to the AW (without suffix, only the name)

Num = 0 "num = 0

e = 1 'is = 0

Do While MN <> "" 'DO while running the following cycles, until the MN value is null

If MN <> AW Then 'If, MN AW value is not equal to the value, runs between IF END IF statement determination

Set Wb = Workbooks.Open (MP & "\" & MN) 'Open the MP \ path under a workbook called MN variable values, and references (Set of role) is assigned Wb ## references assignment if, for a change Wb , was also cited as changed, for details see below PS (3)

a = a + 1 'accumulation of a circulating

With Workbooks (1) .ActiveSheet 'with the use of statements that have been open all of the workbooks in the first workbook is active worksheet

For i = 1 To Sheets.Count 'in all sheet Workbooks (1) .ActiveSheet circulating

If Sheets (i) .Range ( "a1") <> "" Then 'If Workbooks (1) .ActiveSheet A1 cell contents workbook i-th sheet is not empty, the content is determined to be IF

Wb.Sheets (i) .Range ( "a1"). Resize (1, Sheets (i) .UsedRange.Columns.Count) .Copy .Cells (1, 1) 'the workbook wb i-th sheet A1 expanded to cell range line with signs of application columns (x) the size of the region, i.e., A1: x1 region, copying the contents of the expanded region after the Workbooks (1) .ActiveSheet position A1

d = Wb.Sheets (i) .UsedRange.Columns.Count 'wb workbook i-th sheet has traces column count application, and assigned to d

c = Wb.Sheets (i) .UsedRange.Rows.Count - 1 'wb workbook i-th sheet has traces of applications row count, and assigned to c

wn = Wb.Sheets (i) .Name 'the name of the workbook wb i-th sheet assigned to wn

.Cells (1, d + 1) = " table" 'Workbooks (1) .ActiveSheet first row of the worksheet, the d + 1 column of cells filled with "table" string
.Cells (e + 1, d + 1) .Resize (c, 1 ) = MN & wn 'Workbooks (1) .ActiveSheet e + 1 the first row of the worksheet, the first expansion column region d + 1 row is c, an area, and filling in the region is & Wn of the MN
E = E + C
Wb.Sheets (I) .Range ( "A2"). the Resize (C, D) .Copy .Cells (.Range ( "a1048576"). End (xlUp) +. 1 .Row , 1 ') the region of the content copied to Workbooks (1) .ActiveSheet each time from the Workbooks (1) the last non-blank line begins paste .ActiveSheet

End If

The Next
Wbn Wbn = & Chr (13 is) & wb.Name 'Wbn be assigned to the space and with the name of the workbook value Wb of Wbn

Wb.Close False 'Close the workbook Wb
End With
End the If
the MN = Dir' obtained at the matched upper Dir a filename; # dir returns the first matching pathname of a file name. If you want to get other matching pathname of the file name, and once again call Dir, and do not use parameters. If the file is not in line with the conditions, the Dir returns a zero-length string ( ""). Once the return value is zero-length string, and to call Dir again, you must specify pathname, otherwise it will generate an error.
Loop
. The Range ( "A1") the Select 'Select current workbook first cell
Application.ScreenUpdating = True' open screen refresh
MsgBox "were merged" & a & "under all working thin sheets as follows:." & Chr (13) & Wbn, vbInformation, " prompt" 'gives final tips
End Sub

Guess you like

Origin www.cnblogs.com/medik/p/11093885.html