, A value of a column all the class files VBA, a statistical folder (including subfolders) Statistics

1 issue

1.1 a detailed analysis of the problem

  • The problem stems from a group of players in question
  • Statistical need more of the same files in a folder
  • Questioner asking for is csv, in the example I used xlsm
  • The value of a column of statistical tables and statistics show page

Think of the difficulties the initial 1.2

  • Folder statistics for a file is not difficult, but requires an iterative sub-folder, you may need to use recursion
  • With the contents before using VBA file, you must first open the file

 

2 The first edition correct code base can run it, not optimized

2.1 Code demand points

  • I sent amend point that the questioner
  • You change the next parameter
  • 1 path1 directory under the file you want to change the operation of the folder = "C: \ Users \ Administrator \ Desktop \ test1"
  • Under 2 change the type of file you want to check, If f1.Name Like "* .xlsm" Then I checked that your estimate is .xlsm .csv
  • You want to change the statistics under 3 column ThisWorkbook.Worksheets ( "sheet1") Cells (m + 1, 3) .Value = Application.Average (Range (. "A: a") Value). I figure that column A

2.2 Code limitations

  • EXCEL tables open my background processing is not good, try for a long time, also met the active component can not create object and other issues
  • Open efficiency is now a little slow, I deliberately added a delay to avoid jamming
  • Because of this background I open the table the way the code has run, or will open those tables together, if the number of multi-table memory may be stuck to. . .
  • other problems

2.3 Code of ideas

  • Why add a layer to call sub?
  • Because the main sub is need to use to call their own, but because calls itself recursively time again, different files need to reference the path to the folder, you can not repeatedly call the old path. So just let the subject SUB also arguments being invoked.
  • Why use a module-level variable
  • But also because of recursion, itself calls itself, will lead to the main process will be repeated calls, but if the process-level variables will be reset each time the value of the variable, this is obviously undesirable character demands, but after each recursive call itself, value We will continue to accrue.
  • Design ideas
  • This code is layered: the file level \ wb layer \ sh layer \ content layer
Dim m

Sub test1()
path1 = "C:\Users\Administrator\Desktop\test1"
Call test2(path1)
End Sub
 
 
Sub test2(path1)

Dim fso1 As Object
Set fso1 = CreateObject("scripting.filesystemobject")
 
Application.ScreenUpdating = False
Dim f1 As Object
Dim sh1 As Object
 

For Each f1 In fso1.GetFolder(path1).Files
    If f1.Name Like "*.xlsm" Then
'          Debug.Print f1.Name
          Workbooks.Open (f1.Path)
          Workbooks(f1.Name).Worksheets("sheet1").Cells(1, 9) = 888
          Application.Wait (Now + TimeValue("00:00:01"))
          For Each sh1 In Workbooks(f1.Name).Worksheets
              ThisWorkbook.Worksheets("sheet1").Cells(m + 1, 1).Value = f1.Name
              ThisWorkbook.Worksheets("sheet1").Cells(m + 1, 2).Value = sh1.Name
              ThisWorkbook.Worksheets("sheet1").Cells(m + 1, 3).Value = Application.Average(Range("a:a").Value)
              m = m + 1
          Next
    End If
Next
 
Dim subfd1
For Each subfd1 In fso1.GetFolder(path1).SubFolders
    Call test2(subfd1.Path)
Next
 
 
Application.ScreenUpdating = True
End Sub

 

 

3 to solve the course and learning points

3.1 sub recursive call their own problems

  • Recursion is circulating, but a kind of special cycle, at least in theory, is convergent
  • And the lower limit of the normal range for loop or do loop conditions such as recursive calls himself this cycle, in theory, there is the end of the
  • For example, I wrote this subfolder, always a limited number.
  • Recursion is certainly not infinite infinite loop
  • Infinite loop, can only be wrong cycle.

 

3.2 contains the parameters of the sub () can not be run directly, only by another sub () or function () call

  • Later, I direct sub itself with the parameters (parameter must), then calls itself recursively, they also put on the path parameter (subfolders parameters)
  • Then rack up a higher level of sub, sub I call this process the data

 

3.3 type parameters, how do optional parameters? --- now only mandatory parameters plus

  • Remember parameters, there is a mandatory parameter, optional parameters?
  • But how to set a mandatory parameter, optional parameters? Will not get, you have to try
  • Later, I direct sub itself with the parameters (parameter must), then calls itself recursively, they also put on the path parameter (subfolders parameters)
  • Then rack up a higher level of sub, sub I call this process the data

 

The entry into force range of parameters and choose to use 3.4

  • Because I wrote this code relate to, calls itself (recursively)
  • So if only sub-level variable process, the next call will reset itself variable m
  • If you use a static m, can be achieved repeatedly calls also continues to count. But the downside is that, unless the application is completely shut down all of EXCEL, or even close the current code thisworkbook, variable or not clear.
  • Finally, the module-level variable Dim m into the front module, the calling module may pass so m

 

3.5 VBA document processing system ----- too much, did not thoroughly understand the difference

Under what circumstances as these in the end what each has its applicable conditions with feeling

  • createobject(file.path)
  • fso 和 fso.opentextfile ()
  • fso sum fso.getfile ()
  • fso 和 fso.getfolder ()

 

  • Tried various forms open in the background of very different methods ----
  • Set wb1 = fso1.getfile(f1.Name)
  • Set wb1 = fso1.opentextfile(f1.path, 2)
  •  Set wb1 = CreateObject(f1.path)
  • Set wb1 = Workbooks.Open(f1.path)

 

  • Set objFSO = CreateObject("Scripting.FileSystemObject")
  • Set objFile = objFSO.CreateTextFile("c:\windows\xxwl.ini")  
  • Set wb1 = CreateObject(f1.path)

 

3.6 file type identification

  • If there are multiple file types, which have the following wording is sometimes different, reasons unknown
  • wb1.worksheets
  • workbooks(wb1.name).worksheets

 

3.7 treatment EXCEL file, you must first open the corresponding file --- otherwise it will error

  • If the corresponding EXCEL file without first opened in the background, often error: subscript out of range, in fact, is not open table

 

3.8 EXCEL table such documents open in the background

  • Why should it open in the background?
  • Because if the front desk to open the form, the code will interrupt the current table operation.
  • 方法1: application.screenupdating=false/true  + workbooks.open()
  • Method 2: createobject (file.path) this does not work well, used to be possible, I do not know why
  • Method 3: some, open a new application (EXCEL)
  • and many more

 

3.9 vba activeX component can not create object?

 

Published 383 original articles · won praise 45 · Views 100,000 +

Guess you like

Origin blog.csdn.net/xuemanqianshan/article/details/104045255