Guest User

Untitled

a guest
Jun 21st, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.57 KB | None | 0 0
  1. Option Explicit
  2. Sub copydata()
  3. Dim script As Object
  4. Dim catalogue As Object
  5. Dim textfile As Object
  6. Dim loadedfile As Workbook
  7. Dim actualfile As Workbook
  8. Dim path As String
  9. Dim column_index As Integer
  10. path = InputBox("Please input path")
  11. Application.ScreenUpdating = False
  12. Set actualfile = ActiveWorkbook
  13. Set script = CreateObject("Scripting.FileSystemObject")
  14. Set catalogue = script.GetFolder(path)
  15. Application.DisplayAlerts = False
  16. Application.AskToUpdateLinks = False
  17. For Each textfile In catalogue.Files
  18.  
  19. Workbooks.Open textfile
  20. Set loadedfile = ActiveWorkbook
  21. loadedfile.Worksheets(1).Range("D1:D15").Copy
  22. column_index = actualfile.Worksheets(1).Range("A5").CurrentRegion.Columns.Count
  23. actualfile.Worksheets(1).Range("A5").Offset(0, column_index).PasteSpecial
  24. loadedfile.Close Savechanges:=False
  25.  
  26. Next textfile
  27.  
  28. Application.ScreenUpdating = True
  29. Application.DisplayAlerts = True
  30. Application.AskToUpdateLinks = True
  31.  
  32. End Sub
  33.  
  34. Sub Main()
  35.  
  36. Dim path As String
  37. path = GetFolder("") & ""
  38.  
  39. Debug.Print path
  40. End Sub
  41.  
  42.  
  43. ' strPath is the initial path
  44. Private Function GetFolder(strPath As String) As String
  45. Dim fldr As FileDialog
  46. Dim sItem As String
  47. Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
  48. With fldr
  49. .Title = "Select a Folder"
  50. .AllowMultiSelect = False
  51. .InitialFileName = strPath
  52. If .Show <> -1 Then GoTo NextCode
  53. sItem = .SelectedItems(1)
  54. End With
  55. NextCode:
  56. GetFolder = sItem
  57. Set fldr = Nothing
  58. End Function
  59.  
  60. Sub Copydata()
  61.  
  62. Dim path As String
  63. path = GetFolder("") & ""
  64.  
  65. Dim script As FileSystemObject
  66. Set script = New FileSystemObject
  67.  
  68. Dim catalogue As Folder
  69. Set catalogue = script.GetFolder(path)
  70.  
  71. Application.ScreenUpdating = False
  72. Application.DisplayAlerts = False
  73. Application.AskToUpdateLinks = False
  74.  
  75.  
  76. Dim textfile As File
  77. Dim column_index As Integer
  78. Dim loadedfile As Workbook
  79.  
  80. Dim actualfile As Workbook
  81. Set actualfile = ActiveWorkbook
  82.  
  83. For Each textfile In catalogue.Files
  84.  
  85. Workbooks.Open textfile
  86. Set loadedfile = ActiveWorkbook
  87. loadedfile.Worksheets(1).Range("D1:D15").Copy
  88. column_index = actualfile.Worksheets(1).Range("A5").CurrentRegion.Columns.Count
  89. actualfile.Worksheets(1).Range("A5").Offset(0, column_index).PasteSpecial
  90. loadedfile.Close Savechanges:=False
  91.  
  92. Next textfile
  93.  
  94. Application.ScreenUpdating = True
  95. Application.DisplayAlerts = True
  96. Application.AskToUpdateLinks = True
  97.  
  98. End Sub
  99.  
  100. ' strPath is the initial path
  101. Private Function GetFolder(strPath As String) As String
  102. Dim fldr As FileDialog
  103. Dim sItem As String
  104. Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
  105. With fldr
  106. .Title = "Select a Folder"
  107. .AllowMultiSelect = False
  108. .InitialFileName = strPath
  109. If .Show <> -1 Then GoTo NextCode
  110. sItem = .SelectedItems(1)
  111. End With
  112. NextCode:
  113. GetFolder = sItem
  114. Set fldr = Nothing
  115. End Function
  116.  
  117. Sub Pull_Data_from_Excel_with_ADODB()
  118.  
  119. Dim cnStr As String
  120. Dim rs As ADODB.Recordset
  121. Dim query As String
  122.  
  123. Dim fileName As String
  124. fileName = "C:tempfile1.xlsm"
  125.  
  126. cnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
  127. "Data Source=" & fileName & ";" & _
  128. "Extended Properties=Excel 12.0"
  129.  
  130. query = "SELECT * FROM [Sheet1$D1:D15]"
  131.  
  132. Set rs = New ADODB.Recordset
  133. rs.Open query, cnStr, adOpenUnspecified, adLockUnspecified
  134.  
  135. Cells.Clear
  136. Range("A2").CopyFromRecordset rs
  137. Dim cell As Range, i As Long
  138. 'headers
  139. With Range("A1").CurrentRegion
  140. For i = 0 To rs.Fields.Count - 1
  141. .Cells(1, i + 1).Value = rs.Fields(i).Name
  142. Next i
  143. .EntireColumn.AutoFit
  144. End With
  145. End Sub
  146.  
  147. Option Explicit
  148.  
  149. Sub Copydata()
  150.  
  151. Dim path As String
  152. ' retrieve the path to the folder with the files to pull data from
  153. path = GetFolder("") & ""
  154.  
  155. Dim script As FileSystemObject
  156. Set script = New FileSystemObject
  157.  
  158. Dim catalogue As Folder
  159. Set catalogue = script.GetFolder(path)
  160.  
  161. Application.ScreenUpdating = False
  162. Application.DisplayAlerts = False
  163. Application.AskToUpdateLinks = False
  164.  
  165. Dim cnStr As String
  166. Dim rs As ADODB.Recordset
  167.  
  168. Dim query As String
  169. ' SQL query to pull D1:D15 from Sheet1 in each file
  170. query = "SELECT * FROM [Sheet1$D1:D15]"
  171.  
  172. Dim wbFile As Variant
  173.  
  174. ' iterate through the files in the folder user selected
  175. For Each wbFile In catalogue.Files
  176.  
  177. ' upate the connection string with path to each file
  178. cnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
  179. "Data Source=" & wbFile & ";" & _
  180. "Extended Properties=Excel 12.0"
  181.  
  182. ' populate a recordset with D1:D15 from each file
  183. Set rs = New ADODB.Recordset
  184. rs.Open query, cnStr, adOpenUnspecified, adLockUnspecified
  185.  
  186. ' Copy data from recordset to range in one go
  187. Range("A5").Offset(0, Range("A5").CurrentRegion.Columns.Count).CopyFromRecordset rs
  188.  
  189. ' close the recordset and free memory
  190. rs.Close
  191. Set rs = Nothing
  192. Next wbFile
  193.  
  194. Application.ScreenUpdating = True
  195. Application.DisplayAlerts = True
  196. Application.AskToUpdateLinks = True
  197.  
  198. End Sub
  199.  
  200. ' strPath is the initial path
  201. Private Function GetFolder(strPath As String) As String
  202. Dim fldr As FileDialog
  203. Dim sItem As String
  204. Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
  205. With fldr
  206. .Title = "Select a Folder"
  207. .AllowMultiSelect = False
  208. .InitialFileName = strPath
  209. If .Show <> -1 Then GoTo NextCode
  210. sItem = .SelectedItems(1)
  211. End With
  212. NextCode:
  213. GetFolder = sItem
  214. Set fldr = Nothing
  215. End Function
  216.  
  217. Sub copydata()
  218. On Error GoTo ErrHandler
  219.  
  220. 'declarations...
  221.  
  222. 'other code...
  223.  
  224. CleanExit:
  225. 'this code always executes
  226. Application.ScreenUpdating = True
  227. Application.DisplayAlerts = True
  228. Application.AskToUpdateLinks = True
  229. Exit Sub
  230.  
  231. ErrHandler:
  232. 'Display message to the user
  233. MsgBox "Error " & Err.Number & " : " & Err.Description, vbCritical, "Unexpected Error!"
  234. Resume CleanExit
  235. End Sub
  236.  
  237. Option Explicit
  238.  
  239. Sub copydata()
  240.  
  241. Dim path As String
  242. path = InputBox("Please input path")
  243.  
  244. Application.ScreenUpdating = False
  245.  
  246. Dim actualfile As Workbook
  247. Set actualfile = ActiveWorkbook
  248.  
  249. Dim script As Object
  250. Set script = CreateObject("Scripting.FileSystemObject")
  251.  
  252. Dim catalogue As Object
  253. Set catalogue = script.GetFolder(path)
  254.  
  255. Application.DisplayAlerts = False
  256. Application.AskToUpdateLinks = False
  257.  
  258. Dim textfile As Object
  259. For Each textfile In catalogue.Files
  260.  
  261. Workbooks.Open textfile
  262.  
  263. Dim loadedfile As Workbook
  264. Set loadedfile = ActiveWorkbook
  265.  
  266. loadedfile.Worksheets(1).Range("D1:D15").Copy
  267.  
  268. Dim column_index As Integer
  269. column_index = actualfile.Worksheets(1).Range("A5").CurrentRegion.Columns.Count
  270.  
  271. actualfile.Worksheets(1).Range("A5").Offset(0, column_index).PasteSpecial
  272. loadedfile.Close Savechanges:=False
  273.  
  274. Next textfile
  275.  
  276. Application.ScreenUpdating = True
  277. Application.DisplayAlerts = True
  278. Application.AskToUpdateLinks = True
  279.  
  280. End Sub
  281.  
  282. Private Sub EnableExcelUI(Optional ByVal enabled As Boolean = True)
  283. Application.ScreenUpdating = enabled
  284. Application.DisplayAlerts = enabled
  285. Application.AskToUpdateLinks = enabled
  286. End Sub
  287.  
  288. 'Set xFolder = objFSO.GetFolder(xDir)
  289. 'ERROR with Scripting.FileSystemObject
  290. 'Run-time Error 76 Path Not Found
  291.  
  292.  
  293. Set xFolder = xMainZip.GetFolder(xDir)
  294. 'ERROR with Shell.Application
  295. 'Run-time Error 438 Object Doesn't support this Property or Method
Add Comment
Please, Sign In to add comment