Я довольно новичок в VBA *, и я хотел бы получить вашу помощь по следующей проблеме: я использую следующий код и для двух первых случаев (ну, для первых двух IF) код работает отлично, я получаю таблицы из Access импортированы в мой файл Excel. Однако код не работает в случае двух других ПЧ (EDIP 2003 V1.06 / Default и EPD (2013) V1.04). Всякий раз, когда я пытаюсь использовать одну из двух для импорта соответствующей таблицы, Excel сообщает (ошибка 1004): сбой источника данных инициализации
Excel выделяет строку
.Refre sh BackgroundQuery: = False
* Я инженер-архитектор, и я использовал VBA для разработки инструмента расчета для зданий в больницах на основе Excel для своей кандидатской диссертации. Итак, я совсем не знаком с кодами VBA!
Вот код:
Sub MacroTest()
'
' MacroTest Macro
'
'
If Worksheets("General").Range("C4").Value = "AWARE" Then
ActiveWorkbook.Queries.Add Name:="DB_AWARE_impact_indicators", Formula _
:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Access.Database(File.Contents(""C:\Users\mbruyninckx\Dropbox\LCA-BREEAM tool\LCA-BREEAM\__VK MAT 01 LCA tool\_Database\Access database\Database_LCA_tool.accdb""), [CreateNavigationProperties=true])," & Chr(13) & "" & Chr(10) & " _DB_AWARE_impact_indicators = Source{[Schema="""",Item=""DB_AWARE_impact_indicators""]}[Data]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " _DB_AWARE_impact_indicators"
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""DB_AWARE_impact_indicators"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [DB_AWARE_impact_indicators]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "DB_AWARE_impact_indicators"
.Refresh BackgroundQuery:=False
ActiveSheet.Name = "DB_AWARE_impact_indicators"
End With
ElseIf Worksheets("General").Range("C4").Value = "CED (Cumulative Energy Demand)" Then
ActiveWorkbook.Queries.Add Name:="DB_CED_impact_indicators", Formula _
:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Access.Database(File.Contents(""C:\Users\mbruyninckx\Dropbox\LCA-BREEAM tool\LCA-BREEAM\__VK MAT 01 LCA tool\_Database\Access database\Database_LCA_tool.accdb""), [CreateNavigationProperties=true])," & Chr(13) & "" & Chr(10) & " _DB_CED_impact_indicators = Source{[Schema="""",Item=""DB_CED_impact_indicators""]}[Data]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " _DB_CED_impact_indicators"
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""DB_CED_impact_indicators"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [DB_CED_impact_indicators]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "DB_CED_impact_indicators"
.Refresh BackgroundQuery:=False
ActiveSheet.Name = "DB_CED_impact_indicators"
End With
ElseIf Worksheets("General").Range("C4").Value = "EDIP 2003 V1.06 / Default" Then
ActiveWorkbook.Queries.Add Name:="DB_EDIP_2003_impact_indicators", Formula _
:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Access.Database(File.Contents(""C:\Users\mbruyninckx\Dropbox\LCA-BREEAM tool\LCA-BREEAM\__VK MAT 01 LCA tool\_Database\Access database\Database_LCA_tool.accdb""), [CreateNavigationProperties=true])," & Chr(13) & "" & Chr(10) & " _DB_EDIP_2003_impact_indicators = Source{[Schema="""",Item=""DB_EDIP_2003_impact_indicators""]}[Data]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " _DB_EDIP_2003_impact_indicators"
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""DB_EDIP_2003_impact_indicators""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [DB_EDIP_2003_impact_indicators]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "DB_EDIP_2003_impact_indicators"
.Refresh BackgroundQuery:=False
ActiveSheet.Name = "DB_EDIP_2003_impact_indicators"
End With
ElseIf Worksheets("General").Range("C4").Value = "EPD (2013) V1.04" Then
ActiveWorkbook.Queries.Add Name:="DB_EPD_2013_V1_04_impact_indicators", Formula _
:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Access.Database(File.Contents(""C:\Users\mbruyninckx\Dropbox\LCA-BREEAM tool\LCA-BREEAM\__VK MAT 01 LCA tool\_Database\Access database\Database_LCA_tool.accdb""), [CreateNavigationProperties=true])," & Chr(13) & "" & Chr(10) & " _DB_EPD_2013_V1_04_impact_indicators = Source{[Schema="""",Item=""DB_EPD_2013_V1_04_impact_indicators""]}[Data]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " _DB_EPD_2013_V1_04_impact_indicators"
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""DB_EPD_2013_V1_04_impact_indicators""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [DB_EPD_2013_V1_04_impact_indicators]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "DB_EPD_2013_V1_04_impact_indicators"
.Refresh BackgroundQuery:=False
ActiveSheet.Name = "DB_EPD_2013_impact_indicators"
End With
End If
End Sub