VBScript删除Excel工作簿

时间:2014-04-22 12:23:56

标签: excel vbscript

下面是实际代码。它正在复制特定行并将其粘贴到新的excel工作簿,然后将其他excel文件复制到新工作簿,然后两个文件将进行协作。我没有得到任何错误。现在我需要添加更多的东西,也就是说,我需要附加当前日期和时间的文件名(MasterDumppath,Finalpath),我还需要删除objworkbook2。 请帮助。

on error resume next
set objexcel = createobject ("Excel.Application")

d = Now()

p = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)

'Suppress the dialog boxes
objExcel.DisplayAlerts = False

Dim objexcel
Dim objworkbook1, objworkbook2,objworkbook3, objworksheet
Dim i,s
Dim startrow,intnewrow
Dim endrow,endrow1,endrow2,endrow3,endrow4,endrow5
Dim countsheet
Dim Row,Row1,Rows

Const xlContinuous = 1
Const xlThick = 4
const xlLeft = 2

MovReportPath = p & "\Copy of Movement Report"
PSAlertpath = p & "\Emails PS Alert"
MasterDumpPath = p & "\Master_Dump"
Finalpath = p & "\Final_Report"

'Creating a excel file for master dump
Set objWorkbook2= objExcel.Workbooks.Add()
objWorkbook2.SaveAs(MasterDumpPath)

'Adding a column Called Region
objworkbook2.worksheets("Sheet1").cells(1,5).value = "Region"
objWorkbook2.Worksheets("Sheet1").Cells(1,5).Font.Bold = True


'opening the file Movement report 
set objworkbook1 = objexcel.workbooks.open(MovReportPath)

'Counting the sheets in Movement Report
countSheet = objworkbook1.Sheets.Count

'initializations
intnewrow=1
i = 1

'Copying data from sheets
for i = 1 to countsheet

Set objWorksheet = objWorkbook1.WorkSheets(i)
objworksheet.Activate

'copy from the 2nd row 
If intNewRow = 1 Then
startrow = 1
Else
startrow = 2
End If

'count the number of used row of every sheet
endrow = objWorkbook1.Worksheets(i).UsedRange.Rows.Count

name = objworkbook1.Sheets(i).Name

if objworkbook1.worksheets(i).cells(1,1) = "Movement Type" then

'copy the data
objWorkbook1.Worksheets(i).Range("A" & startrow &":D"& endrow).copy

'count the rows before pasting
endrow3 = objWorkbook2.Worksheets("sheet1").UsedRange.Rows.Count

'paste it on workbook2
objWorkbook2.Worksheets("Sheet1").Cells(intNewRow,1).Pastespecial

'count the rows after pasting
Row = objWorkbook2.Worksheets("sheet1").UsedRange.Rows.Count
endrow2 = Row + 1

'copying the country names to Region column
select case name

case "UK"
objworkbook2.worksheets("Sheet1").Range("E2" &":E" & endrow2).value = "London"

case "IND"
objworkbook2.worksheets("Sheet1").Range("E" & endrow3 &":E" & endrow2).value = "GSSC Delhi"

case "USA"
objworkbook2.worksheets("Sheet1").Range("E" & endrow3 &":E" & endrow2).value = "Americas"

case "LUX"
objworkbook2.worksheets("Sheet1").Range("E" & endrow3 &":E" & endrow2).value = "Luxembourg"

case "BEL"
objworkbook2.worksheets("Sheet1").Range("E" & endrow3 &":E" & endrow2).value = "Brussels"

case "BRA"
objworkbook2.worksheets("Sheet1").Range("E" & endrow3 &":E" & endrow2).value = "Brussels"

case "UAE"
objworkbook2.worksheets("Sheet1").Range("E" & endrow3 &":E" & endrow2).value = "UAE"

case "AMS"
objworkbook2.worksheets("Sheet1").Range("E" & endrow3 &":E" & Row).value = "Amsterdam"

End select

'increment the row
intNewRow = intNewRow + (endrow - startrow + 1)

End if
next

'Close Movement Report file
objworkbook1.close

'Name the sheet of master dump as master dump
objworkbook2.Sheets(1).Name = "Master Dump"

'Save Master Dump
objworkbook2.save

'open PS Alet file
set objworkbook3 = objexcel.workbooks.open(PSAlertpath)

'Sheetname in PSAlert
Sheetname = objworkbook3.Sheets(1).Name

'Save the file as Final Report
objWorkbook3.SaveAs(Finalpath)

'count the rows in master dump file
Row1 = objWorkbook2.Worksheets("Master Dump").UsedRange.Rows.Count

'copy data from master dump
objworkbook2.worksheets("Master Dump").Range("A2:E" & row1 ).copy

'Close master dump file
objworkbook2.close

'Same sheetname copied to Final Report
sheetname3 = objworkbook3.sheets(1).Name 
sheetname3 = sheetname

'count the rows in Final Report
Rows = objWorkbook3.Worksheets(sheetname3).UsedRange.Rows.Count

endrow4 = Rows + 1

'Paste data of master dumo to Final Report
objworkbook3.worksheets(sheetname3).cells(endrow4,5).PasteSpecial

'count rows of Final report after pasting
endrow5 = objWorkbook3.Worksheets(sheetname3).UsedRange.Rows.Count

'write data to Report type column
for s = endrow4 to endrow5
if objworkbook3.worksheets(sheetname3).cells(s,4).value = "" then 
objworkbook3.worksheets(sheetname3).cells(s,4).value = "Movement Type"
End if
Next

'Format the EmpID cell
objexcel.activesheet.Columns("F:F").Select
objexcel.activesheet.Range("F2").Activate
objexcel.Selection.NumberFormat = "General"


'Removing Duplicates
Macro1
Sub Macro1()
objworkbook3.worksheets(sheetname3).UsedRange.RemoveDuplicates Array(4,5,7),1
End Sub

'Allignment
objworkbook3.Worksheets(sheetname3).usedrange.HorizontalAlignment = xlLeft
'objWorkbook3.Worksheets(sheetname3).usedrange.HorizontalAlignment = -4108

'Bordering
objworkbook3.Activesheet.usedrange.Borders.Linestyle = xlContinuous 
objworkbook3.Activesheet.cells(1,1).select

objworkbook3.save
objworkbook3.close


msgbox "Done"

1 个答案:

答案 0 :(得分:0)

set objworkbook = objexcel.open("C:\Documents and Settings\SupriyaS\Desktop\Report")

这是否需要以正确的扩展名结束,即Report.xlsx