Saving Specific Sheet With VBA

dxzaber

New Member
Joined
Feb 1, 2015
Messages
35
Private Sub CommandButton1_Click()
Dim path As String
Dim filename1 As String
path = ThisWorkbook.path
filename1 = Range("K2").Text
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=filename1 & ".txt", FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True


End Sub



This Code save the whole work book as txt file. But I want to save only specific sheet say sheet4.
 
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Roger Govier

Active Member
Joined
Jun 19, 2002
Messages
303
Hi
Try inserting a line to copy the relevant sheet before making the save.

Private Sub CommandButton1_Click()

Dim path As String
Dim filename1 As String
path = ThisWorkbook.path
filename1 = Range("K2").Text
Application.DisplayAlerts = False
Sheets("Sheet4").Copy
ActiveWorkbook.SaveAs Filename:=filename1 & ".txt", FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True


End Sub
 

dxzaber

New Member
Joined
Feb 1, 2015
Messages
35
Sheets("Sheet4").Copy


Adding this only creates a new copy of sheet4 . Does not even save.
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,493
You have not included the path to the folder in which you want save your file. In this case, the file gets saved in the current directory. So look there for the file. Also, the file format for a text file should be xlText, not xlOpenXMLWorkbook. Accordingly, try the following instead...

Code:
Private Sub CommandButton1_Click()
    Dim path As String
    Dim filename1 As String
    path = ThisWorkbook.path
    filename1 = Range("K2").Text
    Application.DisplayAlerts = False
    Sheets("Sheet4").Copy
    ActiveWorkbook.SaveAs Filename:=path & "\" & filename1 & ".txt", FileFormat:=xlText
    ActiveWorkbook.Close SaveChanges:=False
    Application.DisplayAlerts = True
End Sub

Note that the workbook is closed once it's saved. However, if you do not want the workbook to be closed, you can delete the appropriate line.

Hope this helps!
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,129,317
Messages
5,635,527
Members
416,862
Latest member
MGDlite

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top