Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


VBA copy from Excel to Notepad

Posted on 2013-05-21
Medium Priority
Last Modified: 2013-05-23
Need some code to copy a range of cells from Excel,open up notepad and paste into it.
Anyway to have notepad save to a given csv file and save into a given folder using VBA?
Question by:cebu1014
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 39

Expert Comment

ID: 39184760
It's all possible, but it's not necessarily clean. Why don't you save the csv directly without going through notepad?

LVL 22

Expert Comment

ID: 39184909
Yes, I would suggest using VBA to simply create a new worksheet, paste the desired data into that sheet, then Save-As the sheet using CSV into the desired folder.

Author Comment

ID: 39185044
I would like to at least no how to open up notepad and to paste into it using VBA just to have the know how. There is some issues I am having otherwise when using double quotes in fields .

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 81

Expert Comment

ID: 39185078
If you want to learn how to open Notepad from Excel, paste data and save the resulting file, take a look at the code in this Mr. Excel thread: http://www.mrexcel.com/forum/excel-questions/46163-open-notepad-form-visual-basic-applications.html
LVL 22

Expert Comment

ID: 39185139
A simple way to get started is this, but it won't save the file:

Sub CopyToNotepad()
    Shell "Notepad", vbNormalFocus
    AppActivate "Untitled - Notepad"
    SendKeys "123"
End Sub

Open in new window

LVL 22

Accepted Solution

rspahitz earned 2000 total points
ID: 39185189
Or another way that will create the file then open it, insert data, then save:
Sub CopyToNotepad()
    Dim iFileNumber As Integer
    Selection.Copy' copy the current Excel selection

    iFileNumber = FreeFile()' find the next free file identifier

   ' create the file
    Open "C:\Users\{username}\Documents\abc.txt" For Output As #iFileNumber
    Close #iFileNumber

    ' launch the file in notepad
    Shell "Notepad C:\Users\{username}\Documents\abc.txt", vbNormalFocus

   ' activate the window so we can talk to it
    AppActivate "abc.txt"

   ' send a Ctrl+V (paste) and Ctrl+S (save) to notepad...could also send an Alt-FX to exit if desired
    SendKeys "^v^s"
End Sub

Open in new window

Note that SendKeys is considered occasionally flaky and not always reliable.

Author Comment

ID: 39189909
I got it to work using your above routine as a guide. I removed some of the lines though and added  TRUE at end of sendkeys command in order to get it to work.

Sub CopyToNotepad()
    Range(Selection, Selection.End(xlDown)).Select

    Shell "Notepad C:\Users\mw1\my Documents\abc.csv", vbNormalFocus

    SendKeys "^a", True
    SendKeys "{ENTER}", True
SendKeys ("^{HOME}"), True
SendKeys "^v", True
End Sub

Author Closing Comment

ID: 39189912
Good Job
LVL 22

Expert Comment

ID: 39191225
Glad it got you in the right direction.  Seems that you learned a few new things along the way too.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question