Excel VBA SQL Query on not saved Workbook

huangapple go评论98阅读模式
英文:

Excel VBA SQL Query on not saved Workbook

问题

以下是要翻译的内容:

我有一个包含一些宏的Excel工作簿。使用一个宏,我打开另一个现有的工作簿(其中已经包含一些数据),并向其添加一些行。然后,使用另一个宏(uebergebene_wo),我在该工作簿上执行SQL查询,但查询仅找到磁盘上工作簿中的行,而不是添加的行。如果我在添加行后保存工作簿,就能找到所有数据。
当我将行添加到包含宏的工作簿时,我无需首先保存工作簿。
我如何才能在不保存的情况下从外部工作簿获取数据?

这些是宏的相关部分:(代码部分未翻译)
VBA code

希望这对您有帮助。

英文:

I have an Excel workbook with some macros. With one macro I open another existing workbook (which already contains some data) and add some rows to it. Then, using another macro (uebergebene_wo), I make a SQL query on that workbook, but the query only finds rows that are in the workbook on disk. None of the added rows. If I save the workbook after adding the rows, all the data is found.
When I add the rows to the workbook that the macro is in, I don't need to save the workbook first.
What can I do to get the data from the external workbook without saving?

These are the relevant parts of the macro:

  1. Dim conn As Object 'SQL-Connection
  2. ' Path to the files
  3. Public Const strReportDir As String = "Q:\Reporting\KPIs\"
  4. ' Name of the raw-data-file
  5. Public Const strBewegRoh As String = "Bewegung Rohdaten.xlsx"
  6. Function connect_sql(strWbPath As String)
  7. 'Connect to the Data Source
  8. Set conn = CreateObject("ADODB.Connection")
  9. With conn
  10. .Provider = "Microsoft.ACE.OLEDB.12.0"
  11. .ConnectionString = "Data Source=" & strWbPath & ";" & _
  12. "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
  13. .Open
  14. End With
  15. End Function
  16. Sub uebergebene_wo()
  17. ' Fragt die Daten für Übergebenw WO ab.
  18. Dim rs As Object 'RecordSet
  19. Dim sql As String
  20. Dim start_row As Integer: start_row = 1
  21. Dim start_col As Integer: start_col = 1
  22. Dim iCol As Integer
  23. connect_sql (strReportDir & strBewegRoh) ' Verbindung herstellen
  24. 'write SQL
  25. sql = "Select * from [WO-Bewegung Rohdaten$] Where `Datum Übergabe` BETWEEN 45021 AND 767010"
  26. 'run SQL
  27. Set rs = conn.Execute(sql) 'delete data from previous runs in worksheet "Results" Worksheets("Results").Cells(start_row, start_col).CurrentRegion.Clear
  28. Set wksBewegResults = Workbooks(strBewegRoh).Worksheets("Results")
  29. 'insert header row worksheet "Results" (only if HDR=YES)
  30. For iCols = 0 To rs.Fields.Count - 1
  31. wksBewegResults.Cells(start_row, start_col + iCols).Value = _
  32. rs.Fields(iCols).Name
  33. Next
  34. 'paste result of sql to worksheet "Results"
  35. wksBewegResults.Cells(start_row + 1, start_col).CopyFromRecordset rs
  36. 'clean up and free memory
  37. rs.Close
  38. Set rs = Nothing
  39. conn.Close
  40. Set conn = Nothing
  41. End Sub
  42. </details>
  43. # 答案1
  44. **得分**: 1
  45. *如何在不保存的情况下获取外部工作簿中的数据?*
  46. *答:可能无法*
  47. 但是,如果您的想法是获取这些工作簿中的信息,可能需要使用以下代码保存所有的Excel工作簿:
  48. ```VBA
  49. Sub SaveAllWorkbooks()
  50. Dim wbk As Workbook
  51. For Each wbk In Excel.Workbooks
  52. wbk.Save
  53. Next
  54. End Sub

考虑将此代码添加到Sub uebergebene_wo()下面的行中,就像这样:

  1. Sub uebergebene_wo()
  2. SaveAllWorkbooks
  3. ' 获取传递的WO的数据。
英文:

What can I do to get the data from the external workbook without saving?

Ans: Probably nothing

However, if your idea is to obtain the information in those workbooks, it might be a good idea to save all the Excel workbooks with code:

  1. Sub SaveAllWorkbooks()
  2. Dim wbk As Workbook
  3. For Each wbk In Excel.Workbooks
  4. wbk.Save
  5. Next
  6. End Sub

Consider adding the code on the line below Sub uebergebene_wo()
here, like this:

  1. Sub uebergebene_wo()
  2. SaveAllWorkbooks
  3. &#39; Fragt die Daten f&#252;r &#220;bergebenw WO ab.

huangapple
  • 本文由 发表于 2023年4月6日 19:13:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75948863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定