Thursday, March 8, 2012

how to fetch / copy data from text file to a spreadsheet?

Here I am giving you an idea on how can we fetch or copy the data from a text file to a spreadsheet?


Sub FetchDataFromTextFile()
Dim i As Long
Dim LineText As String
Open "C:\YourPath\YourTextFile.txt" For Input As #24
i = 1
While Not EOF(24)
Line Input #24, LineText
ActiveSheet.Cells(i, 1).Value = LineText
i = i + 1
Wend
Close #24
End Sub


I have not commented on any line expecting you could understand, if not please write a comment or mail me. Thank you.

How to append / add / export data to a text file from a spreadsheet?

Here I will show you how to append / export data to a text file from a spreadsheet?
I am trying to reduce the script as much as possible in all my posts at my level.. however my kind suggestion is - in case if you feel that anything better than or smaller than this, please help me to edit by giving your valuable comments, or please feel free to mail me to ratheesh.ram@live.in
Thank you.


Sub ExportDataToTextFile()
Dim LastRow As Long
Open "C:\YourPath\YourTextFile.txt" For Append As #24
LastRow = Worksheets(1).Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRow
Write #24, Cells(i, 1).Value
Next i
Close #24
End Sub


I have not commented on any line expecting you could understand, if not please write a comment or mail me. Thank you.

Wednesday, March 7, 2012

How to get all the file names from a folder using excel VBA ?

Dear All,

Today I am starting this blog with a very small VBA script for a useful function.
The below procedure will help you to fetch all the file names from a folder or a specified file type from a folder.

Sub GetFileNames()
Dim FName As String
FName = Dir("C:\YourPath\" & "*.*", vbNormal)
'//you can set this as per your wish e.g. for all .xlsx, you can change as "*.xlsx"//'
i = 1
While FName <> ""
Cells(i, 1).Value = FName
i = i + 1
FName = Dir()
Wend
End Sub