Excel VBA How to Copy excel columns into NotePad

jceast7

New Member
Joined
Sep 7, 2019
Messages
5
I have 3 column in and excel vba file that I want to copy into NotPad, which I can do but since the text content in the cells of each column vary in length, NotPad does not maintain the format as copied, but the copy results are altered and not in line as desired. Is there a way to ix this??

Thanks

jceast7
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Re: Excel VBA How to Coply excel columns into NotePad

Try this

Code:
Sub Copy_into_NotePad()
  Dim lr As Long
  lr = Range("A" & Rows.Count).End(xlUp).Row
  Range("A1:C" & lr).Copy
  Shell "notepad.exe", vbNormalFocus
  SendKeys "^V"
End Sub
 
Upvote 0
Re: Excel VBA How to Coply excel columns into NotePad

Try this

Code:
Sub Copy_into_NotePad()
  Dim lr As Long
  lr = Range("A" & Rows.Count).End(xlUp).Row
  Range("A1:C" & lr).Copy
  Shell "notepad.exe", vbNormalFocus
  SendKeys "^V"
End Sub

Thank you for your response. This is your code modified for my sheet and ranges

[/Sub Copy_into_NotePad()
Dim lr As Long
lr = Range("A" & Rows.Count).End(xlUp).row
Sheets("Copied Summary").Range("A1:C" & lr).Copy
Shell "notepad.exe", vbNormalFocus
SendKeys "^V"]
End Sub


I run the code and most of the 3 columns are copied - but not to the end of the data in the columns. I does open NotePad, but it is blank?? No Paste of data.
It copies to row 60 and the data ends at row 153 for this case. But each case varies. Note sure why it does not work. Any suggestions would be greatly appreciated.

A sample of the Data is below:

Customer:ABC
Botavia Ref:123456
Customer Ref:PO123456
Railcar:123458
O.G. , inn:19.00
B.G. , inn:113.50
TOV, GAL:28,297.
TOV, BBL:674.
H2O Allow, %:1.50
H2O Vol. , %:1.50
Ded, Vol:0.00
GOV, BBL:28,297.
Obsr. Temp, Deg-F:75.00
API @ 60-F:30.00
VCF - Table 6:B0.99332
GSV, GAL:28,108.
GSV, BBL:669.24

<tbody>
</tbody>
 
Last edited by a moderator:
Upvote 0
Re: Excel VBA How to Coply excel columns into NotePad

Try this please:

Code:
Sub Copy_into_NotePad()
  Dim lr As Long
  lr = [COLOR=#333333][FONT=Verdana]Sheets("Copied Summary").[/FONT][/COLOR]Range("A" & Rows.Count).End(xlUp).Row
  [COLOR=#333333][FONT=Verdana]Sheets("Copied Summary").[/FONT][/COLOR]Range("A1:C" & lr).Copy
  Shell "notepad.exe", vbNormalFocus
  SendKeys "^V"
End Sub

Note: Run with F5 or put a button on the sheet to run the macro, but don't run the macro with F8 (debug)
 
Upvote 0
Re: Excel VBA How to Coply excel columns into NotePad

I woudn't use SendKeys as it doesn't work consistently and depends on focus plus it turns off the keyboard Numeric pad.

A more reilable way is by using the Sendmessage API :
Code:
Option Explicit
 
 [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
    Private Declare PtrSafe Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As LongPtr, ByVal wFlag As Long) As LongPtr
 [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
 [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If

Private Const WM_PASTE = &H302


Sub Copy_into_NotePad()

  Dim lr As Long
  
  lr = Sheets("Copied Summary").Range("A" & Rows.Count).End(xlUp).Row
  Sheets("Copied Summary").Range("A1:C" & lr).Copy
  Shell "notepad.exe", vbMinimizedNoFocus 'vbNormalFocus
  
  Call SendMessage(GetNextWindow(FindWindow("Notepad", vbNullString), 5), WM_PASTE, True, 0)
  
End Sub
 
Upvote 0
Re: Excel VBA How to Coply excel columns into NotePad

Here is a much bulletproof method that takes into account the possible scenario where there are more than on Notepad window opened .

Code:
Option Explicit 

 [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
    Private Declare PtrSafe Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As LongPtr, ByVal wFlag As Long) As LongPtr
    Private Declare PtrSafe Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As LongPtr, lpdwProcessId As Long) As Long
    Private Declare PtrSafe Function GetParent Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
    Private Declare PtrSafe Function GetShellWindow Lib "user32" () As LongPtr

 [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetShellWindow Lib "user32" () As Long
 [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

Private Const WM_PASTE = &H302
Private Const GW_HWNDPREV = 3



Sub Copy_into_NotePad()

    Dim lr As Long, PID As Long
    
    lr = Sheets("Copied Summary").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("Copied Summary").Range("A1:D" & lr).Copy
    PID = Shell("notepad.exe", vbMinimizedNoFocus)  'vbNormalFocus)
    
    Call SendMessage(GetNextWindow(HwndFromPID(PID), 5), WM_PASTE, True, 0)
    
End Sub



[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then
    Private Function HwndFromPID(ByVal PID As Long) As LongPtr
        Dim lTempHwnd As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
    Private Function HwndFromPID(ByVal PID As Long) As Long
        Dim lTempHwnd As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

        Dim lTempPid As Long
        
        lTempHwnd = GetShellWindow
        Do While lTempHwnd
            If GetParent(lTempHwnd) = 0 Then
                Call GetWindowThreadProcessId(lTempHwnd, lTempPid)
                If lTempPid = PID Then
                    HwndFromPID = lTempHwnd
                    Exit Do
                End If
            End If
            lTempHwnd = GetNextWindow(lTempHwnd, GW_HWNDPREV)
            DoEvents
        Loop
    
End Function
 
Last edited:
Upvote 0
Re: Excel VBA How to Coply excel columns into NotePad

If you only need the 3 columns in a txt file:

Code:
Sub Macro2()
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  Columns("A:C").Copy
  Workbooks.Add
  Range("A1").PasteSpecial Paste:=xlPasteValues
  ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & "book1.txt", FileFormat:=xlText
  ActiveWorkbook.Close False
End Sub
 
Upvote 0
Re: Excel VBA How to Coply excel columns into NotePad

Hello - I would like to post my code that I generated from your input but I do not know how. Can you please tell me hoe to do this?
I did get the first code you provided to work well, however it does disable the NUMLOCK key. Is the a way arounf this???

Thank you
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,268
Members
448,558
Latest member
aivin

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