写入新工作表的值未保存

时间:2014-06-18 10:30:39

标签: excel vbscript

我有以下脚本:

Set fso = CreateObject("Scripting.FileSystemObject")
Set xl  = CreateObject("Excel.Application")
xl.Visible = True  'set to False for production
Set wb = xl.Workbooks.Open("C:\XXXSample_Data.xlsx") 'xl.Workbooks.Open(f.Path)
Set ws = wb.Sheets("XXXXX")
Set objRE1 = New RegExp
    With objRE1
        .Pattern    = "Invalid Response received from system"
        .IgnoreCase = True
        .Global     = False
    End With

    Set objRE2 = New RegExp

    With objRE2
       .Pattern    = "Enterprise Data mapping cannot be found"
       .IgnoreCase = True
       .Global     = False
    End With

    Set objRE3 = New RegExp

    With objRE3
       .Pattern    = "Caught exception and rethrowingError detected"
       .IgnoreCase = True
       .Global     = False
    End With

    xf = 0
    re = 0
    gg = 0
    rst = 0

    For Each cell In ws.UsedRange.Columns(11).Cells 
        str = cell.Value
        If objRE1.Test( str ) Then
                xf = xf + 1
        Elseif objRE2.Test( str ) Then
                re = re + 1
        Elseif  objRE3.Test( str ) Then
                gg = gg + 1
        Else
                rst = rst + 1
        End If      
    Next

    Set objWorksheet = xl.ActiveWorkbook.Worksheets(2)
    objWorksheet.Cells(5,5).Value  = "Invalid Response received from system "
    objWorksheet.Cells(6,5).Value  = "Enterprise Data mapping cannot be found "
    objWorksheet.Cells(7,5).Value  = "Caught exception and rethrowingError detected "
    objWorksheet.Cells(8,5).Value  = "Errors With new String patterns found "

    'WScript.Echo xf
    objWorksheet.Cells(5,6).Value = xf 
    objWorksheet.Cells(6,6).Value = re 
    objWorksheet.Cells(7,6).Value = gg
    objWorksheet.Cells(8,6).Value = rst

    wb.Saved = True
    wb.Close

Set objRE1 = Nothing
Set objRE2 = Nothing
xl.Quit

第二张表中的值未写入或保存。

另一点 - 我看到文件以某种方式保存在“我的文档”下。也许有人可以指出。

1 个答案:

答案 0 :(得分:1)

wb.Save
wb.Close true, "C:\XXXSample_Data.xlsx"

设置wb.Saved = True会将工作簿标记为已保存而不保存。

Close的参数是:保存更改(true)和保存更改的文件路径