Avatar of brasiman
brasiman
 asked on

Macro to a specific sheet in Excel as different workbook

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!
VB ScriptVisual Basic ClassicSpreadsheetsMicrosoft Excel

Avatar of undefined
Last Comment
brasiman

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Elena Quinn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
brasiman

ASKER
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.
Elena Quinn

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?
Elena Quinn

Just to confirm, you are saving to an existing directory, correct?
Your help has saved me hundreds of hours of internet surfing.
fblack61
brasiman

ASKER
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!
brasiman

ASKER
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!
SOLUTION
Subodh Tiwari (Neeraj)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
brasiman

ASKER
Unfortunately still no luck. When in Sheet1, I run the macro, and it doesn't work.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.