Save inactive worksheet as text file using active worksheet cell for filename

I have an excel workbook consisting of 2 sheets.
Sheet 1 = Entry
Sheet 2 = Compiled
What I'd like to have happen is to have a macro to save a copy of the Compiled sheet as a text file with a filename identified in Sheet1 cell  C2.

The end result being a text copy of the Compiled sheet saved with a button click so I can continue working on the Entry form to create yet another. I've tried several variants on this theme on here and google found, but I lack the ability to modify it enough to meet my need so am hoping for help.

Thanks!
K_R_MAsked:
Who is Participating?
 
Rory ArchibaldCommented:
For example:
Sub SaveTextCopy()
   Dim wks As Worksheet
   Set wks = Worksheets("Compiled")
   wks.SaveAs Sheets("Entry").Range("C2").Value & ".txt", xlTextWindows
End Sub

Open in new window

0
 
K_R_MAuthor Commented:
Works perfectly, thank you very much. I can clearly see where I was mistaken now. As a brief follow up question, how can I better define the directory I wish to save to? My attempt below fails

 
Sub SaveTextCopy()
   Dim wks As Worksheet
   Dim path As String
   path = "C:\Flags\"
   Set wks = Worksheets("Compiled")
   wks.SaveAs path & Sheets("Entry").Range("C2").Value & ".txt", xlTextWindows
End Sub 

Open in new window


0
 
Rory ArchibaldCommented:
Fails in what way?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
K_R_MAuthor Commented:
Okay, Once I run the macro, the filename that I am working on and the Compiled sheet itself, changes to the Cell C2 name and causes the macro to fail on the next attempt. Is there a save as copy method so that the original file is unchanged?
0
 
Rory ArchibaldCommented:
No - there is a SaveCopyAs method, but that is for the workbook. You can do this though:
Sub SaveTextCopy()  
   Dim wks As Worksheet  
   Dim path As String  
   path = "C:\Flags\"  
   Set wks = Worksheets("Compiled")
   wks.Copy
   activeworkbook.SaveAs path & thisworkbook.Sheets("Entry").Range("C2").Value & ".txt", xlTextWindows
   activeworkbook.close false
End Sub 

Open in new window

0
 
K_R_MAuthor Commented:
That did the trick, original file is intact, new text file is where it belongs. Thanks again, very well done!
0
 
Rory ArchibaldCommented:
Just to tidy up a bit:
Sub SaveTextCopy()   
   Dim wks As Worksheet   
   Dim path As String   
Application.ScreenUpdating = False
   path = "C:\Flags\"   
   Set wks = Worksheets("Compiled") 
   wks.Copy 
   activeworkbook.SaveAs path & thisworkbook.Sheets("Entry").Range("C2").Value & ".txt", xlTextWindows 
   activeworkbook.close false 
Application.ScreenUpdating = True
End Sub 

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.