大家早上好
我正在尝试在Excel中创建一个表单,该表单将X个单元格的内容(在下面的POC中我仅使用一个)传递为使用Microsoft Office加载项的SAS存储过程中的参数。我在单个查询上执行此任务没有任何问题,但是,当我更改A1中的值并再次提交代码时,会出现以下弹出框,因为上一个查询的结果已经出现在单元格A10中:>
查询
Sub InsertStoredProcessWithPrompts()
Dim sas As SASExcelAddIn
Set sas = Application.COMAddIns.Item("SAS.ExcelAddIn").Object
'Delete and entries in the existing log
sas.ClearLog
'Setting Options
sas.options.ResetAll
sas.options.AutoInsertResultsIntoDocument = True
sas.options.PromptForParametersOnRefreshMultiple = False
sas.options.ShowStatusWindow = False
'Specify the Cell Used For Parameter Input
Dim age As Range
Set age = Sheet1.Range("A1")
'Capture The Prompts To Be Used As Parameters Within The Stored Process
Dim prompts As SASPrompts
Set prompts = sas.CreateSASPromptsObject
prompts.Add "AGE", age
Dim stp As SASStoredProcess
Set stp = sas.InsertStoredProcess("/User Folders/scmitchell/My Folder/Test Streams", Sheet1.Range("A10"), prompts)
End Sub
第二个执行结果
因此,我添加了一个If语句来标识与存储过程关联的对象是否已经存在。如果确实创建了存储过程,那么如果没有创建存储过程,则修改存储过程的参数以在AGE提示中包括新定义的值。
查询
Sub InsertStoredProcessWithPrompts()
Dim sas As SASExcelAddIn
Set sas = Application.COMAddIns.Item("SAS.ExcelAddIn").Object
If stp Is Nothing Then
'Delete and entries in the existing log
sas.ClearLog
'Setting Options
sas.options.ResetAll
sas.options.AutoInsertResultsIntoDocument = True
sas.options.PromptForParametersOnRefreshMultiple = False
sas.options.ShowStatusWindow = False
'Specify the Cell Used For Parameter Input
Dim age As Range
Set age = Sheet1.Range("A1")
'Capture The Prompts To Be Used As Parameters Within The Stored Process
Dim prompts As SASPrompts
Set prompts = sas.CreateSASPromptsObject
prompts.Add "AGE", age
Dim stp As SASStoredProcess
Set stp = sas.InsertStoredProcess("/User Folders/scmitchell/My Folder/Test Streams", Sheet1.Range("A10"), prompts)
Else
Set stp = stp.Modify("Test Streams")
End If
End Sub
第一个或后续执行结果
以下是突出显示的问题:
Dim stp As SASStoredProcess
我可能会朝着完全错误的方向前进,所以请随时提供替代解决方案,但是到目前为止,以上内容似乎还算合理。我从StackOverflow帖子(VBA check if object is set)中提取了“如果对象”存在的代码,但是也许由于我很少使用VBA而犯了一个基本错误。
任何帮助将不胜感激。
致谢。
斯科特