引用 Excel 对象时需要对象错误 [英] Object Required Error when referencing Excel Objects

查看:21
本文介绍了引用 Excel 对象时需要对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

所需对象:Activesheet

Object required:Activesheet

这是代码:

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:UsersvijendraDesktop	est2.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 

推荐答案

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

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

进入这个:

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

这篇关于引用 Excel 对象时需要对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆