Runtime error "424" "object required" when trying to copy sheet

I get this error in the line below. What am I doing wrong?

Runtime Error '424' required object

Sub GetSheets()
Path = "C:\Users\vinod\Desktop\dt kte\"
Filename = Dir(Path & "*.xls")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
Sheets("Payout Summary").Select
Sheet.Copy After:=ThisWorkbook.Sheets(1) ' <~~~~ Error occurs here
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
+4
source share
2 answers

An error occurs because Sheet- Nothing is Empty. You did not specify any variable Sheet; when you use it for the first time, the default option is used, whose value is Empty.

Change the faulty line to:

ActiveSheet.Copy After:=ThisWorkbook.Sheets(1) 

Although really, you should read the following: How to Avoid Using Select in Macros Excel VBA

Option Explicit , . , .

+3

- :

Sheets("Payout Summary").Select
Sheet.Copy After:=ThisWorkbook.Sheets(1)

Sheets("Payout Summary").Copy After:=ThisWorkbook.Sheets(1)

""!!! , -, . :)

0

Source: https://habr.com/ru/post/1568112/


All Articles