VB脚本格式化excel

时间:2017-06-05 14:46:17

标签: excel vba excel-vba

我有一个由另一个程序生成的excel文件。因此,它是Microsoft Excel工作簿,而不是启用宏的工作簿。

我想写一个VB脚本,它对excel文件进​​行基本格式化,而不必调用嵌入式宏。

这可能吗?我一直在搜索,但只发现了一个调用嵌入式宏的VB脚本(如下所示):

VB脚本:

Sub TestMacro()
'example
End Sub

调用宏:

    Dim maxDate As Variant

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sqlinsert As String

maxDate = DMax("[Eff Date]", "400_CF_BREAK_LOG")
Set db = CurrentDb


Set rs = db.OpenRecordset("860_APPEND_DIFFERENCES")
If Not rs.BOF Then
    'populate the table
    rs.MoveFirst
    Do
        If (rs![Eff Date] > maxDate Or IsNull(maxDate)) Then
            sqlinsert = "INSERT INTO 400_CF_BREAK_LOG (Eff Date, PrimarySecurity ID Number, CUSIP(Aladdin ID), IsrName, Asset Type, Metlife Port Code, Business Unit, Principal Difference, Total PAM Principal, Total Aladdin Principal,Income Difference, Total PAM Interest,Total Aladdin Interest,Total CF Difference,Total PAM CF,PAM Coupon)" & _
            " VALUES ('" & rs("Eff Date") & "', '" & rs("PrimarySecurity ID Number") & "', '" & rs("CUSIP(Aladdin ID)") & "', '" & rs("IsrName") & "', '" & rs("Asset Type") & "', '" & rs("Metlife Port Code") & "', '" & rs("Business Unit") & "', '" & rs("Principal Difference") & "',  '" & rs("Total PAM Principal") & "',  '" & rs("Total Aladdin Principal") & "','" & rs("Income Difference") & "', '" & rs("Total PAM Interest") & "', '" & rs("Total Aladdin Interest") & "', '" & rs("Total CF Difference") & "', '" & rs("Total PAM CF") & "', '" & rs("PAM Coupon") & "') "
            DoCmd.RunSQL (sqlinsert)
        End If
        rs.MoveNext
    Loop Until rs.EOF
End If

1 个答案:

答案 0 :(得分:0)

一个常见的技巧是使用两个工作簿:

  • Master.xlsm
  • TEST.XLS

Master将包含以下宏:

  1. 创建或打开test.xls
  2. 修改并格式化test.xls
  3. 保存并关闭测试,xls