引用Excel对象时出现对象所需的错误

时间:2014-03-15 18:34:42

标签: excel vbscript

下面是VBScript代码,用于从一个excel工作簿中删除重复项,然后将其复制到另一个,当我运行它时,我收到错误;

  

所需对象:活动表

这是代码:

Set objExcel = CreateObject("Excel.Application")    

' Open the workbook
Set objWorkbook1 = objExcel.Workbooks.Open("C:\Users\vijendra\Desktop\duplicates.xlsx")

Macro

Sub Macro1()
  activesheet.UsedRange.RemoveDuplicates Columns=Array(), Header=xlYes
End Sub

'Opening the 2nd workbook
Set objworkbook2 = objExcel.Workbooks.Open("C:\Users\vijendra\Desktop\test2.xlsx")

'Set to True or False, whatever you like
objExcel.Visible = True

'Select the range on Sheet1 you want to copy 
objWorkbook1.Worksheets("Sheet1").usedrange.Copy

'Paste it on sheet1 of workbook2, starting at A1
objWorkbook2.Worksheets("Sheet1").Range("A1").PasteSpecial

'Activate Sheet2 so you can see it actually pasted the data
objWorkbook2.Worksheets("Sheet1").Activate 

1 个答案:

答案 0 :(得分:1)

VBScript不支持隐式使用应用程序对象。你必须明确地使用它。 VBScript也不支持命名参数(name:=value)或Excel内置常量(xlYes)。改变这一行:

activesheet.UsedRange.RemoveDuplicates Columns=Array(), Header=xlYes

进入这个:

objExcel.ActiveSheet.UsedRange.RemoveDuplicates Array(), 1