Object Required Error when referencing Excel Objec

2020-01-29 03:10发布

Below is the VBScript code for deleting the duplicates from one excel workbook and then copying it to another, when I run it I'm getting an error;

Object required:Activesheet

This is the code:

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条回答
▲ chillily
2楼-- · 2020-01-29 03:18

VBScript doesn't support implicit use of the application object. You have to use it explicitly. VBScript also doesn't support named arguments (name:=value) or Excel built-in constants (xlYes). Change this line:

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

into this:

objExcel.ActiveSheet.UsedRange.RemoveDuplicates Array(), 1
查看更多
登录 后发表回答