SAS ODS tagsets.excelxp:在SAS中调用vbscript的autofit列宽

时间:2017-02-27 14:24:18

标签: excel excel-vba vbscript sas vba

每周我输出一个yyyymmdd_report.xls工作簿,其中包含多个工作表,文件名中包含我的宏日期。如果我手动打开此工作簿,以下vbs脚本可以自动调整列:

Sub AutoFitAll() 
    Application.ScreenUpdating = False 
    Dim wkSt As String 
    Dim wkBk As Worksheet 
    wkSt = ActiveSheet.Name 
    For Each wkBk In ActiveWorkbook.Worksheets 
        On Error Resume Next 
        wkBk.Activate 
        Cells.EntireColumn.AutoFit 
    Next wkBk 
    Sheets(wkSt).Select 
    Application.ScreenUpdating = True      
End Sub 

有什么方法可以把它放到SAS内的某种调用中,我不必手动执行此操作?我在网上找到的文档似乎有点太复杂,无法满足我的需求。逻辑是:

1. Point to/open output report .xls file (maybe some command/DDE method?)
2. Run vbs script above
3. Re-save the file 

我正在运行SAS v9.4并拥有MSOffice 2010.

2 个答案:

答案 0 :(得分:2)

电子表格标记语言doesn't support autofit on text columns有点令人失望。但是,我认为这可能适合您的具体情况:

将以下代码保存到autofit.vbs

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False
Set objWorkbook = objExcel.Workbooks.Open(WScript.Arguments.Item(0))
Set xlmodule = objworkbook.VBProject.VBComponents.Add(1) 

strCode = _
"Sub AutoFitAll() "                                 & vbCr & _
"    Application.ScreenUpdating = False "           & vbCr & _
"    Dim wkSt As String "                           & vbCr & _
"    Dim wkBk As Worksheet "                        & vbCr & _
"    wkSt = ActiveSheet.Name "                      & vbCr & _
"    For Each wkBk In ActiveWorkbook.Worksheets"    & vbCr & _
"        On Error Resume Next "                     & vbCr & _
"        wkBk.Activate "                            & vbCr & _
"        Cells.EntireColumn.AutoFit "               & vbCr & _
"    Next wkBk "                                    & vbCr & _
"    Sheets(wkSt).Select "                          & vbCr & _
"    Application.ScreenUpdating = True"             & vbCr & _
"End Sub"

xlmodule.CodeModule.AddFromString strCode

objExcel.Run "AutoFitAll"

objworkbook.VBProject.VBComponents.Remove xlmodule

objWorkbook.SaveAs WScript.Arguments.Item(0) , -4143
objExcel.Quit

这将打开一个Excel文件,添加宏,运行宏,删除宏,最后将文档保存为正确的XLS(没有像打开带有XLS扩展名的XML文件时的警告提示)。

然后在SAS程序的x命令中利用此VBScript,如下所示:

ods tagsets.ExcelXP file="C:\test.xls";
    proc print data=sashelp.Cars;
    run;
ods tagsets.ExcelXP close;

options xwait;
x "C:\autofit.vbs ""C:\test.xls""";

答案 1 :(得分:0)

<script type="text/javascript" src="cordova.js"></script>

在Excel VBA文件(MyMacro.xlsm)中,您应该定义一个宏,其中包含您要对特定或活动工作簿执行的操作。

例如:

options noxwait noxsync;

/* run Excel, also you can run it manually */
x '"C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE"';

/* wait, Excel is starting */
data _null_;
   rc = sleep(5);
run;

/* create a connection between SAS and Excel */
filename cmds dde 'excel|system';

data _null_;
   file cmds;
   /* open a file with a macro */
   put '[open("C:\MyMacro.xlsm")]';
   /* run the macro MyMacro in MyMacro file */
   put '[run("MyMacro.xlsm!MyMacro")]';
   /* close the macro file */
   put '[close(0)]';
   /* quit Excel */
   put '[quit()]';
run;