Guest User

Untitled

a guest
Oct 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. For Each objFile In objFolder.Files
  2. Set tgtSheet = ThisWorkbook.Sheets.Add
  3. tgtSheet.Name = objFile.Name
  4.  
  5. On Error Resume Next
  6. With tgtSheet.QueryTables.Add(Connection:="TEXT;" & objFile, Destination:=tgtSheet.Range("A1"))
  7. .TextFileParseType = xlDelimited
  8. .TextFileCommaDelimiter = True
  9. .Refresh
  10. End With
  11. On Error GoTo 0
  12.  
  13. ' ------ Force data formatting onto columns
  14. lastRow = FindLastCell(tgtSheet).Row
  15. For i = 1 To UBound(titleArray)
  16. currCol = ColumnByTitle(tgtSheet, titleArray(i))
  17. With tgtSheet
  18. Set columnRange = .Range(.Cells(1, currCol), Cells(lastRow, currCol)).EntireColumn
  19. columnRange.NumberFormat = formatArray(i)
  20. End With
  21. Next i
  22. Next objFile
  23.  
  24. With ThisWorkbook
  25. strPath = .Path
  26. strFile = .FullName
  27. strFileTemp = strPath & "DBtemp" & ".xlsb"
  28. .Worksheets(arrSheets).Copy
  29. End With
  30.  
  31. For i = LBound(arrSheets) To UBound(arrSheets)
  32. If arrSheets(i) <> ActiveSheet.Name Then
  33. If strSQL = "" Then
  34. strSQL = "SELECT * FROM [" & arrSheets(i) & "$]"
  35. Else
  36. strSQL = strSQL & " UNION ALL SELECT * FROM [" & arrSheets(i) & "$]"
  37. End If
  38. End If
  39. Next i
  40.  
  41. ' ------ set up connection string
  42. strCon = _
  43. "ODBC;" & _
  44. "DSN=Excel Files;" & _
  45. "DBQ=" & strFileTemp & ";" & _
  46. "DefaultDir=" & strPath & ";" & _
  47. "DriverId=790;" & _
  48. "MaxBufferSize=2048;" & _
  49. "PageTimeout=5"
  50.  
  51. Set pc = ThisWorkbook.PivotCaches.Add(SourceType:=xlExternal)
  52.  
  53. ' ------ test pivot table to play around with
  54. With pc
  55. .Connection = strCon
  56. .CommandType = xlCmdSql
  57. .CommandText = strSQL
  58. Set pt = .CreatePivotTable(TableDestination:=ActiveSheet.Range("A1"))
  59. pt.Name = "TestPivot"
  60. End With
Add Comment
Please, Sign In to add comment