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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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
Back
Top