+ Reply to Thread
Results 1 to 3 of 3

Print lines to a text file based on change in cell contents

Hybrid View

  1. #1
    Registered User
    Join Date
    09-05-2012
    Location
    Savannah, GA
    MS-Off Ver
    Excel 2010
    Posts
    2

    Print lines to a text file based on change in cell contents

    I have a spreadsheet containing data in this format:

    0 -390 0:00
    -390 -390 1:00
    -390 -390 2:00
    -390 -390 3:00
    -390 -390 4:00
    -390 -140 5:00
    -140 -140 6:00
    -140 -140 7:00
    .
    .
    .
    .
    .
    .
    -140 -100 23:00
    -100 0 24:00

    I need to print all the lines where the value of the second column changes to a different number. I need to print the first line by default. Then, I would need to print the line when the -390 changes to -140 and the lines when the -140 becomes -100. More explicitly, I would need to print the contents of the line with -140. How would I do this? In addition, once the text file is created, is there a way to programmatically allow the user to view the file? I hope my explanation is clear. Any help would be appreciated. I am new to Excel VBA programming.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Print lines to a text file based on change in cell contents

    with data starting from row 2
    Sub ExportSomeLinesToText()
    Set Zona = ActiveSheet.UsedRange
    nrow = Zona.Rows.Count
    ncol = Zona.Columns.Count
    DestFile = "C:\test\test.txt"
    FileNum = FreeFile()
    Open DestFile For Output As #FileNum
    For RowCount = 2 To nrow ' data starting from row 2
      If Cells(RowCount, 1).Value <> Cells(RowCount - 1, 1).Value Then
        For ColumnCount = 1 To ncol
          Print #FileNum, Cells(RowCount, ColumnCount).Text & " ";
          If ColumnCount = ncol Then
            Print #FileNum,
          Else
    'Print #FileNum, ";"; ' optional separator
          End If
        Next
      End If
    Next
    Close #FileNum
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    09-05-2012
    Location
    Savannah, GA
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Print lines to a text file based on change in cell contents

    Thanks very much for your help. This has solved my problem.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1