VBA- merge multiple files using the DIR function

How object assigned to the variable

The Test Sub () 
Dim Worksheet of As SHT 
SHT = Sheets.Add   ' here will complain of 
the Set SHT = Sheets.Add' assign object to a variable need to add the SET 
sht.Name = " April " 
End Sub

Example: Create a table with cell contents for sheet1 name

Sub test()
Dim sht As Worksheet
Dim i As Integer
For i = 2 To 5
    Set sht = Sheets.Add
    sht.Name = Sheet1.Range("a" & i)
Next
End Sub

dir function

  1) There is no such file dir judgment in support wildcards

Sub test()
Dim i As Integer
For i = 1 To 5
    If Dir("a:\data\" & Sheet1.Range("a" & i) & ".xls*") = "" Then     #支持通配符
        Sheet1.Range("b" & i) = "无此文件"
    Else
        Sheet1.Range("b" & i) = "有文件"
    End If

Next
End Sub

  2) dir instructions for use, a plurality of documents, such as Suzhou Suzhou .xlsx, .xls file data in Suzhou  

SS Sub () 
the Range ( " A1 " ) the Dir = ( "D : \ Data \ Suzhou * * .xls " ) # a Suzhou Dir ( "d:. \ Data \ * *") across all the files 
the Range ( " A1 " ) = the Dir returns # Suzhou 
the Range ( " A1 " ) = the Dir # return empty 
the Range ( " A1 " ) = the Dir # error 
End Sub

  3) through all the file names

Sub test1()
Dim str As String
str = Dir("d:\data\*.xls*")
For i = 1 To 100
    Range("a" & i) = str
    str = Dir
    If str = "" Then
        Exit For
    End If
Next
End Sub

  4) shell, the fixed data file folder open and close the

Sub test1()
Dim str As String
Dim wb As Workbook
str = Dir("d:\data\*.xls*")
For i = 1 To 100
    Set wb = Workbooks.Open("d:\data\" & str)
    
    
    
    
    wb.Close
    str = Dir
    If str = "" Then
        Exit For
    End If
Next
End Sub

 

Guess you like

Origin www.cnblogs.com/xiao-xuan-feng/p/12662631.html