Help getting info from excel to textfile

I am very new at VBA, but most of the time I can understand what is going on in a program. I just have trouble creating the program to do what I need.

I need a Program that will read an Excel File that is no longer than 6 columns and no longer than 400 rows. I need the program to print each row into a different line in a text file, and I need everything in each row separated by commas.

Does anyone know how to do this?
The more comments in the code the better...I am still learning

Comments

  • [code]
    Option Explicit

    Private mvarExcel As Object 'Excel.Application
    Private Workbook As Object

    Private Sub Command1_Click()
    'INIT EXCEL OBJECT
    Set mvarExcel = CreateObject("Excel.Application") 'New Excel.Application
    'HIDE EXCEL
    mvarExcel.Visible = False
    'OPEN EXCEL FILE
    mvarExcel.Workbooks.Open "d:ook1.xls"
    End Sub

    Private Sub Command2_Click()
    Dim myRow As Integer
    Dim myCol As Integer
    Dim wb As Object 'Workbook
    Dim ws As Object 'Worksheet

    Dim File1 As Integer
    'GET FREEFILE
    File1 = FreeFile
    'OPEN TEXT FILE TO WRITE
    Open "d:mytxt.txt" For Output As #File1

    'EXCEL ACTIVEWORKBOOK, ACTIVESHEET
    Set wb = mvarExcel.Application.ActiveWorkbook
    Set ws = wb.ActiveSheet

    'GET VALUES FROM CELLS
    For myRow = 1 To 400
    For myCol = 1 To 6
    'OUTPUT TO TEXT FILE
    Print #File1, ws.Rows.Cells(myRow, myCol);
    If myCol < 6 Then Print #File1, ","; Else Print #File1, vbCr
    Next myCol
    Next myRow

    'CLOSE TEXT FILE
    Close File1

    'QUIT EXCEL
    mvarExcel.Quit
    Set mvarExcel = Nothing
    End Sub

    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion