+ Reply to Thread
Results 1 to 3 of 3

VBA: Loop through cells in matrix and return column- and row headers

Hybrid View

Søren Larsen VBA: Loop through cells in... 06-24-2012, 02:48 PM
StevenM Re: VBA: Loop through cells... 06-24-2012, 03:21 PM
Søren Larsen Re: VBA: Loop through cells... 06-25-2012, 08:43 AM
  1. #1
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    VBA: Loop through cells in matrix and return column- and row headers

    I'm struggling with a piece of code I'm working on.

    I want to loop through each cell of one table and compare their column headers (text) and row headers (dates) with the contents of another table.

    Currently my code looks like this:
    Sub test()
    
        Dim cDummy          As Object
        Dim rvDummy         As Date
        Dim cvDummy         As Long
        
        Dim c1Historik      As Object
        Dim c2Historik      As Date
            
        For Each cDummy In Range("rDummy")
            rvDummy = Range(cDummy).End(xlToLeft).Value 'Here is my break-point
            cvDummy = Range(cDummy).End(xlUp).Value
            
            For Each c1Historik In Range("Historik_start")
                c2Historik = Range(c1Historik).Offset(0, 1).Value
                If Range(c1Historik).Offset(0, -1).Value = cvDummy Then
                    If c1Historik.Value <= rvDummy And c2Historik >= rvDummy Then
                        cDummy = 3
                    End If
                End If
            Next c1Historik
        Next cDummy
    
    End Sub
    Any help and suggestions are much appreciated!
    Sincerely
    S?ren Larsen

    "Give a man a fish, and you'll feed him for a day. Give a man a fishing rod, and he'll steal your yacht!"

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: VBA: Loop through cells in matrix and return column- and row headers

    I don't know what your code does, but cDummy and c1Historik should be declared to be As Range, and then they should be treated as a range.

    Assuming that "rDummy" is a name of a range, then:

    For Each cDummy In Range("rDummy")
    cDummy is a one-cell Range inside Range("rDummy"), as such it acts like a Range variable.

    rvDummy = cDummy.End(xlToLeft).Value
    This will return the value of the first cell with data to the left of cDummy.Address.

  3. #3
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: VBA: Loop through cells in matrix and return column- and row headers

    Thanks; I see where I got it wrong! For good measure; here is the final and working code:
    Sub test()
    
        Dim cDummy          As Range
        Dim rvDummy         As Date
        Dim cvDummy         As String
        
        Dim c1Historik      As Range
        Dim c2Historik      As Date
            
        For Each cDummy In Range("rDummy")
            On Error Resume Next
                rvDummy = cDummy.End(xlToLeft).Value
                cvDummy = cDummy.End(xlUp).Value
            
                For Each c1Historik In Range("Historik_start")
                    c2Historik = c1Historik.Offset(0, 1).Value
                    If c1Historik.Offset(0, -1).Value = cvDummy Then
                        If DateValue(c1Historik.Value) <= DateValue(rvDummy) And DateValue(c2Historik) >= DateValue(rvDummy) Then
                            cDummy = 3
                        End If
                    End If
                Next c1Historik
            On Error GoTo 0
        Next cDummy
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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