0

I have a code written for one excel sheet in the form of Macro, In this macro, I am generating a copy of current workbook to a different location. Now, I need to access this copied excel workbook to delete some of its Worksheets from the macro. can anyone tell me how to access the newly copied sheet from the current excel sheet macro?

1 Answer 1

0

The following code will allow you to edit a copy of your workbook:

Sub test()
    Dim wb As Workbook
    Dim strName as String

    strName = "" & ActiveWorkbook.Name
    ActiveWorkbook.SaveCopyAs Filename:=strName
    Set wb = Workbooks.Open(strName)

    Application.DisplayAlerts = False 'Prevents that user is asked when sheets are deleted
    wb.Worksheets("Sheet1").Delete
    Application.DisplayAlerts = True

    wb.Close SaveChanges:=True
End Sub
7
  • Hi Peter, Thank you for such a prompt response. But I think I did not get what you are trying to say. I need to access another workbook from my current workbook(behind which the macro is written). could you please tell me how can this be done with the method you suggested? The error I'm getting on trying this suggested code is related to type mismatch. Jan 29, 2013 at 9:00
  • Ah, I see - sorry! How do you create the copy of the worksheet? Jan 29, 2013 at 9:07
  • using this line - ActiveWorkbook.SaveCopyAs FileName:="<file_path>" & ActiveWorkbook.Name Jan 29, 2013 at 9:08
  • well, I have used same line to open the copied workbook. It isn't working. I am passing filepath and filename with it's extention to workbook.open; the same way as you have done. Getting error 1004 application defined or object defined error. Jan 29, 2013 at 9:24
  • I just modified it to certainly use the same name, please check if this works. If not: does it really save the workbook? Is it already open? Jan 29, 2013 at 9:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.