|Excel And SQL .com|
Home > VBA Macros
How to transpose a column of values into a row
To work with Files in Excel macros, you need to add a reference to Microsoft Scripting Runtime Library. To do that while in VBA editor, go to menu option Tools -- References. When References dialog box is displayed, scroll through the list of libraries to locate "Microsoft Scripting Runtime" and check the checkbox next to it.
This demo assumes that you have a text file named qqq.txt in the folder c:\temp\. You can run the sample with or without the file present and see how it behaves.
Sub Demo() Dim oFileSystem As FileSystemObject Set oFileSystem = New FileSystemObject Dim oFile As TextStream Dim filePath As String filePath = "C:\Temp\qqq.txt" If oFileSystem.FileExists(filePath) Then On Error GoTo Err Set oFile = oFileSystem.OpenTextFile(filePath) MsgBox oFile.ReadAll oFile.Close Else MsgBox "Invalid File Path.", vbCritical, vbNullString Exit Sub End If Exit Sub Err: MsgBox "Error reading the file.", vbCritical, vbNullString oFile.Close Exit Sub End Sub
|© All Rights Reserved. ExcelAndSql.com|