Traversing a matrix in excel using VBA

Posted on 2006-06-01
Last Modified: 2008-02-01
I have data in the following matrix format

ColumnAHeader  ColumnBHeader  ColumnCHeader  ColumnDHeader
     H                               1               1                        
     I                                                 1
     J                                1                                                1

The code should traverse Column A , and print out a row of data in a txt file as follows:
HColumnBHeader =  1, HColumnCHeader = 1, IColumnCHeader = 1,   JColumnBHeader = 1, JColumnDHeader =1                  

Please give complete code, including outputting to txt file. Increasing points for urgency! Thanks in advance.
Question by:vlakhani
    1 Comment
    LVL 35

    Accepted Solution

    Hi vlakhani,

    Sub vlakhani()
     Dim vFF As Long, vFile As String, CLL As Range, uRG As Range, CLL2 As Range, tempStr As String
     vFile = "C:\output.txt"
     Set uRG = Intersect(ActiveSheet.UsedRange, Range(Cells(2, 2), Cells(Rows.Count, Columns.Count)))
     If uRG Is Nothing Then Exit Sub
     vFF = FreeFile
     Open vFile For Output As #vFF
     For Each CLL In uRG.EntireRow.Columns(1).Cells
      If Len(Trim(CLL.Text)) > 0 Then
       For Each CLL2 In Intersect(CLL.EntireRow, uRG).Cells
        If Len(Trim(CLL2.Text)) > 0 Then
         tempStr = tempStr & IIf(Len(tempStr) > 0, ", ", "") & CLL.Text & _
          CLL2.EntireColumn.Cells(1).Text & " = " & CLL2.Value
        End If
      End If
     Print #vFF, tempStr
     Close #vFF
    End Sub


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now