我试图引用从Outlook导出的.csv
文件...我尝试过的代码是错误Run-time error '424': Object Required
我猜它是因为它不是.xls
,.xlsm
等格式?我如何将其作为Worksheet
?
Sub test1()
Dim PrimaryWB As Workbook
Dim DataWS As Worksheets
Set PrimaryWB = ThisWorkbook
Set DataWS = Active.Worksheet
Debug.Print PrimaryWB.Name
Debug.Print DataWS.Name
End Sub
我的活动窗口是.csv
文件,而ThisWorkbook
正在引用我要将数据复制到的工作簿。错误发生在Set DataWS = Active.Worksheet
答案 0 :(得分:2)
Set DataWS = Active.Worksheet
语法无效。正确的sytnax是Set DataWS = ActiveSheet
然而,使用ActiveSheet
可能会有很多陷阱,尽管在少数情况下它是绝对必要的。这不是其中之一。
请参阅此完全限定的代码,以确保所有内容都符合您的要求。
Sub test1()
Dim PrimaryWB As Workbook
Dim DataWB As Workbook
Set PrimaryWB = ThisWorkbook
Set DataWB = Workbooks("myCSV.csv") 'Change name as needed
Dim DataWS as Worksheet
Set DataWS = DataWB.Sheets(1) 'CSV only has one sheet, so index is okay
Debug.Print PrimaryWB.Name
Debug.Print DataWS.Name
End Sub