• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 600
  • Last Modified:

Traversing a matrix in excel using VBA

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.
1 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now