Advertisement
Guest User

Untitled

a guest
Nov 1st, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2. Dim ws1 As Worksheet
  3. Dim ws2 As Worksheet
  4. Dim sql As String
  5. Dim qtb As QueryTable
  6. Dim it As String
  7. Dim pm As String
  8.  
  9.  
  10.  
  11.  
  12. Sub refCol()
  13.  
  14. Application.ScreenUpdating = False
  15. 'Returns or sets the number of sheets that Microsoft Excel automatically inserts into new workbooks.
  16. Application.SheetsInNewWorkbook = 2
  17.    
  18. 'creates a new workbook which becomes the active workbook.
  19. Workbooks.Add
  20.    
  21. 'create two new sheets and assign them to variables so we can reference them
  22. Set ws1 = Sheets("Sheet1")
  23. Set ws2 = Sheets("Sheet2")
  24.  
  25. ws1.Cells(1, 1) = "Number"
  26. ws1.Cells(2, 1) = 1
  27. ws1.Cells(3, 1) = 1
  28. ws1.Cells(4, 1) = 4
  29. ws1.Cells(5, 1) = 1
  30. ws1.Cells(6, 1) = 3
  31. ws1.Cells(7, 1) = 1
  32. ws1.Cells(8, 1) = 1
  33.  
  34. sql = "SELECT ws1.[F1] " & _
  35.       "FROM [" & Worksheets(1).Name & "$] AS ws1"
  36.  
  37. 'sql = "Select Number" & _
  38.       "FROM [" & ws1.Name & "$] as ws1"
  39.      
  40. ws2.Select
  41.      
  42. Call RunQuery
  43.  
  44. End Sub
  45.  
  46. Sub RunQuery()
  47.  
  48.  
  49. With ActiveSheet.QueryTables.Add(Connection:="ODBC;DSN=DSN;UID=USER;HDR=NO", _
  50.             Destination:=Range("A1"))
  51.         .CommandText = sql
  52.         .FieldNames = True
  53.         .RowNumbers = False
  54.         .FillAdjacentFormulas = False
  55.         .PreserveFormatting = True
  56.         .RefreshOnFileOpen = False
  57.         .BackgroundQuery = False
  58.         .RefreshStyle = xlInsertDeleteCells
  59.         .SavePassword = False
  60.         .SaveData = True
  61.         .AdjustColumnWidth = False
  62.         .RefreshPeriod = 0
  63.         .PreserveColumnInfo = True
  64.         .Refresh BackgroundQuery:=False
  65.         End With
  66.         For Each qtb In ActiveSheet.QueryTables
  67.         qtb.Delete
  68.         Next
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement