如何让工作簿B调用的工作簿A中LoadQueries宏在B中执行?
问题:调用其他工作簿的宏时,宏始终在原工作簿执行
想在工作簿B中调用工作簿A里的LoadQueries宏,于是在B中写了Call LoadQueries,但执行后发现这个宏始终在工作簿A里运行,无法在工作簿B中完成操作。
工作簿A中的LoadQueries宏代码
Sub LoadQueries() ' TestQueries Macro Sheets.Add After:=ActiveSheet Sheets("Feuil1").Select Sheets("Feuil1").Name = "questions" Sheets.Add After:=ActiveSheet Sheets("Feuil2").Select Sheets("Feuil2").Name = "clean" Sheets("questions").Select Application.CutCopyMode = False With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _ "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=questions;Extended Properties="""""" _ , Destination:=Range("$A$1")).QueryTable .CommandType = xlCmdSql .CommandText = Array("SELECT * FROM [questions]") .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .PreserveColumnInfo = True .ListObject.DisplayName = "Tableau_questions" .Refresh BackgroundQuery:=False End With Sheets("clean").Select Application.CutCopyMode = False With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _ "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=clean;Extended Properties="""""" _ , Destination:=Range("$A$1")).QueryTable .CommandType = xlCmdSql .CommandText = Array("SELECT * FROM [clean]") .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .PreserveColumnInfo = True .ListObject.DisplayName = "Tableau_clean" .Refresh BackgroundQuery:=False End With End Sub
工作簿B中的调用代码
Call LoadQueries
问题原因
原宏代码大量使用ActiveSheet、Sheets("Feuil1")这类未指定工作簿的引用,当从工作簿B调用时,宏的执行上下文仍绑定在工作簿A中,所有操作自然会作用在A上。要让宏在工作簿B中执行,必须修改宏,让它明确接受目标工作簿作为参数,且所有工作表、范围操作都指向该工作簿。
解决方案
1. 修改工作簿A中的LoadQueries宏,添加目标工作簿参数
Sub LoadQueries(targetWB As Workbook) ' TestQueries Macro Dim wsQuestions As Worksheet Dim wsClean As Worksheet ' 在目标工作簿中添加并命名工作表 Set wsQuestions = targetWB.Sheets.Add(After:=targetWB.ActiveSheet) wsQuestions.Name = "questions" Set wsClean = targetWB.Sheets.Add(After:=wsQuestions) wsClean.Name = "clean" ' 在questions工作表插入查询表 Application.CutCopyMode = False With wsQuestions.ListObjects.Add(SourceType:=0, Source:= _ "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=questions;Extended Properties="""""" _ , Destination:=wsQuestions.Range("$A$1")).QueryTable .CommandType = xlCmdSql .CommandText = Array("SELECT * FROM [questions]") .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .PreserveColumnInfo = True .ListObject.DisplayName = "Tableau_questions" .Refresh BackgroundQuery:=False End With ' 在clean工作表插入查询表 Application.CutCopyMode = False With wsClean.ListObjects.Add(SourceType:=0, Source:= _ "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=clean;Extended Properties="""""" _ , Destination:=wsClean.Range("$A$1")).QueryTable .CommandType = xlCmdSql .CommandText = Array("SELECT * FROM [clean]") .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .PreserveColumnInfo = True .ListObject.DisplayName = "Tableau_clean" .Refresh BackgroundQuery:=False End With End Sub
2. 修改工作簿B中的调用代码,指定目标为自身
' 调用工作簿A的宏,传入当前工作簿B作为操作目标 Call Workbooks("工作簿A.xlsm").LoadQueries(ThisWorkbook)
注意:将
工作簿A.xlsm替换为实际的工作簿A文件名,且确保工作簿A处于打开状态。
内容的提问来源于stack exchange,提问作者EmmBr
相关产品推荐
相关产品推荐

