写入新工作表的值不会被保存

我有以下脚本:

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 

第二个工作表中的值没有被写入或保存。

还有一点 – 我看到文件被保存在“我的文档”之下。 也许这也是有人可以指出的。

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

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

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