Thank you for Visiting my Blog

Saturday, 17 June 2017

How to write data into Notepad from Excel using VBA



Below is the code to write data into notepad from Excel using write function. write function will delimit the data and write into the notepad but It will write data with double quotes.


Sub Writenotepadfile()

'------------Declaring variables
Dim TxtFile As String,
rng As Range,
TextValue As Variant,
i As Integer,
j As Integer


'Provide the notepad text file name with path


TxtFile = ActiveWorkbook.Path & "\result.txt"

'Select the range or parameterized it as per the requirement



Range("A1:E6").Select
Set rng = Selection
  
Open TxtFile For Output As #1

For i = 1 To rng.Rows.Count
    For j = 1 To rng.Columns.Count
   
    TextValue = rng.Cells(i, j).Value
   
    If j = rng.Columns.Count Then
  
    Write #1, TextValue
Else

    Write #1, TextValue,
 End If


Next j
Next i
Close #1

End Sub


 
 
 


No comments:

Post a Comment