2

I need to use an excel object(which is set in a procedure), to other procedure in the same module . But not able to do it. Please help me doing the same. Below is my code.

Public Sub FirstProc() 'Here the excel object is defined

Set xlApp = CreateObject("Excel.Application")
Set xlWorkbook = xlApp.Workbooks.Open(ReportTemplateDirectory\Test.xlsx")
xlApp.Visible = True
...
... 'all work goes here FINE
...

SecondProc  'calling second proc
End Sub

Below is my second procedure,

Public Sub SecondProc()

' In this procedure i need to delete a sheet of the same excel file which is generated in above procedure.

xlWorkbook.Sheets(4).Delete

End Sub

But I am getting the RunTime Error 424 Object Required. Any help would be appreciated.

1 Answer 1

3

While I would just add that line to the first module your could try something like this (tested in Outlook).

DisplayAlertsturned off to avoid a check message when the sheet is deleted.

Public Sub FirstProc() 'Here the excel object is defined

Dim xlApp As Object
Dim xlWorkbook As Object

Set xlApp = CreateObject("Excel.Application")
xlApp.displayalerts = False
Set xlWorkbook = xlApp.Workbooks.Add
xlApp.Visible = True

Call SecondProc(xlWorkbook)    'calling second proc
xlApp.displayalerts = True
End Sub


Public Sub SecondProc(xlWorkbook As Object)
' In this procedure i need to delete a sheet of the same excel file which is generated in above procedure.
xlWorkbook.Sheets(2).Delete
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.