Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
610 views
in Technique[技术] by (71.8m points)

vbscript - Object Required Error when referencing Excel Objects

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:UsersvijendraDesktopduplicates.xlsx")

Macro

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

'Opening the 2nd workbook
Set objworkbook2 = objExcel.Workbooks.Open("C:UsersvijendraDesktopest2.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 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...