Sunny D'Souza
Sunny D'Souza

Reputation: 626

Same Excel macro giving error under different situations

I have 2 Sub() Functions in Excel. Both have different names but same code. but when trying to run together gives an 'Application defined' error. Am not able to understand where the problem is

Following is the code for the same

Sub GT456T()

Sheets("Sheet2").Rows("1:1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Rows("11:11").Select
ActiveSheet.Paste

End Sub



Sub CopyRecord()


Sheets("Sheet2").Rows("1:1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Rows("11:11").Select
 ActiveSheet.Paste
End Sub

I dont understand why does it give error when run one after one another. if i run the 1st macro it doesnt throw an error but when executing the second immediately after the first it throw an 'Application defined' error. please help

Upvotes: 0

Views: 296

Answers (1)

Without more details about the problem, this is guesswork, but here's my shot at it:

The .Select method only works on ranges whose parent sheet is currently active. When you run either macro once, Sheet1 ends up being the active sheet when it's done running. Then you try to run either macro again "immediately" after the first, and the macro wants to select something on Sheet2, but Sheet2 isn't active. Sheet1 is. So you get an error.

There, that was my best, most educated guess.

Upvotes: 3

Related Questions