-
Notifications
You must be signed in to change notification settings - Fork 11
/
Excel VBA - Files.vb
105 lines (75 loc) · 1.97 KB
/
Excel VBA - Files.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
http://software-solutions-online.com/list-files-and-folders-in-a-directory/
https://stackoverflow.com/questions/31414106/get-list-of-excel-files-in-a-folder-using-vba
Activate 'Microsoft Scripting Runtime'
Option Explicit
Sub get_eachFilesInFolder()
'
' This macro gets each files in a given folder
'
Dim o As FileSystemObject
Dim p As Folder
Dim f As file
Dim a As Variant
Set o = New FileSystemObject
Set p = o.GetFolder("D:\Applications")
For Each a In p.Files
Debug.Print a
'Or
'Get names of files
Debug.Print Dir(a, vbNormal)
Next
set f = Nothing
set o = Nothing
set p = Nothing
End Sub
Sub get_aSpecificFile()
'
' This macro gets each files in a given folder
'
Dim o As FileSystemObject
Dim p As Folder
Dim f As file
Dim a As Variant
Dim file_Path as String
file_Path = Workbooks(ActiveBook.Path).Path
Set o = New FileSystemObject
Set p = o.GetFolder("D:\Applications")
Set f = p.Files("...")
'TO DO
set f = Nothing
set o = Nothing
set p = Nothing
End Sub
Sub get_eachFilesInFolder()
'
' Other method
'
Dim o As FileSystemObject
Dim p As Folder
Dim h As Files
Dim f As file
Dim a As Variant
Dim open_File As String
open_File = "Comptage" & ".xlsm"
Set o = New FileSystemObject
Set p = o.GetFolder("C:\Users\Zadig\Documents")
Set h = p.Files
Set f = h.Item("...")
'TO DO
Set o = Nothing
Set p = Nothing
Set f = Nothing
End Sub
'
' Import each Excel files from a folder to access
'
Sub ImportfromPath(path As String, intoTable As String, hasHeader As Boolean)
Dim fileName As String
'Loop through the folder & import each file
fileName = Dir(path & "\*.xls")
While fileName <> ""
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, intoTable, path & fileName, hasHeader
'check whether there are any more files to import
fileName = Dir()
Wend
End Sub