Paste data in a new notepad File, save it and close it

Suril

New Member
Joined
Oct 10, 2013
Messages
6
Dear All,

I want to open a notepad file from my excel file, paste a range of cells from my excel file to the current notepad file, name the notepad file same as my current file name and ultimately save it on either Desktop or the same path where my excel file exists and close the notepad file.

Would be very grateful for some light on the same.

PS: i have the code for copying the required data - once this data is copied from Excel, i use the following code:
With Application
Shell "notepad.exe", 3
SendKeys "^v"
VBA.AppActivate .Caption
.CutCopyMode = False
End With

However, this doesnt activate the notepad file, plus I want to name this notepad and close it
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Code:
Sub rangeExport()
    Application.DisplayAlerts = False
    Range("A1:D5").Copy ' change range
    Sheets.Add after:=Sheets(Sheets.Count)
    ActiveSheet.Paste
    ActiveSheet.Copy
    With ActiveWorkbook.Sheets(1)
        .SaveAs "C:\test\test.txt", xlTextMSDOS ' change filename
        .Parent.Close False
    End With
    ActiveSheet.Delete
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Thanks for the code Patel. Unfortunately my workbook is going to be locked, hence this code wont be of much use. Can I do this without adding another worksheet? Also, is there a way in which I can save the notepad file on the users' desktop - be it any user.

Code:
Sub rangeExport()
    Application.DisplayAlerts = False
    Range("A1:D5").Copy ' change range
    Sheets.Add after:=Sheets(Sheets.Count)
    ActiveSheet.Paste
    ActiveSheet.Copy
    With ActiveWorkbook.Sheets(1)
        .SaveAs "C:\test\test.txt", xlTextMSDOS ' change filename
        .Parent.Close False
    End With
    ActiveSheet.Delete
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
I added a new sheet because I don't know if you have an empty sheet for service, suppose it is Sheet2
Code:
Sub rangeExport()
    Application.DisplayAlerts = False
    Range("A1:D5").Copy Sheets(2).Range("A1")
    Sheets(2).Copy
    With ActiveWorkbook.Sheets(1)
        .SaveAs "C:\test\test.txt", xlTextMSDOS ' change filename
        .Parent.Close False
    End With
    ActiveSheet.Delete
    Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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