Macro to a specific sheet in Excel as different workbook

brasiman
brasiman used Ask the Experts™
on
I have a button that runs a macro, but it isn't working. When I click the button, i am trying to save Sheet2 as filename "Import.csv" in the "C:\ToImport" folder. So the steps I manually take are:

1. Click on Sheet2
2. File>Save As>"Import.csv"

I would like to keep my original .mxlsx spreadsheet open so I can do more with it. Any help writing this macro would be appreciated! Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sheets("Sheet2").Select
    Sheets("Sheet2").Copy
    ActiveWorkbook.SaveAs Filename:="C:\toImport\Import.csv", _
        FileFormat:=xlCSV, CreateBackup:=False

Open in new window

Author

Commented:
Thanks EQUINN. That unfortunately didn't work. Do I need Sub and End Sub at the begging and end? I tried it a few ways and it just takes me to Sheet2 but doesn't save the file.
Sorry, yes you do need a Sub and End Sub before and after.  

It works for me.  Why don't you post your code and we can have a look?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Just to confirm, you are saving to an existing directory, correct?

Author

Commented:
Thanks EQUINN for looking at this. Here's my code:

Sub Import()
Sheets("Sheet2").Select
    Sheets("Sheet2").Copy
    ActiveWorkbook.SaveAs Filename:="C:\ToImport\Import.csv", _
        FileFormat:=xlCSV, CreateBackup:=False
End Sub

I have a button on Sheet1 that runs the macro. I don't know if that makes any difference. Correct, i am saving it to an existing directory. Thanks!

Author

Commented:
Ok, so when I run the macro in Sheet2, it works. But when I click the button on Sheet1, it doesn't work. It just takes me to Sheet2 but doesn't do anything. How do I get this so I can click the button on Sheet1? Also, how do I get it so the new created spreadsheet doesn't open. I don't need it to open. Thanks!
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
It should work.
Try to remove Sheets("Sheet2").Select and see if that resolves the issue.

Author

Commented:
Unfortunately still no luck. When in Sheet1, I run the macro, and it doesn't work.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial