Sunday, June 17, 2012

Write Excel Data into a Text File

Hi friends,

I have a new small code for you all who are learning Excel VBA.
This piece of code writes the data which is in excel sheet, into a
text file.



Here is the code:


Private Sub btnWrite_Click()
  Dim id, name As String
  Dim salary As Double
  Dim fName As String
  Dim fNumber As Integer
  Dim lRow As Long
  
  fName = "E:\MyData\EmpData.txt"
  fNumber = FreeFile
  
  Open fName For Output As #fNumber
  lRow = 2
  
  Do
    With Sheet17
     id = .Cells(lRow, 1)
     name = .Cells(lRow, 2)
     salary = .Cells(lRow, 3)
    End With
    
    Write #fNumber, id, name, salary
    lRow = lRow + 1
  Loop Until IsEmpty(Sheet17.Cells(lRow, 1))
  Close #fNumber
End Sub

No comments:

Post a Comment