+ Reply to Thread
Results 1 to 7 of 7

How to loop through columns

Hybrid View

  1. #1
    Registered User
    Join Date
    12-10-2013
    Location
    usa
    MS-Off Ver
    Excel 2013
    Posts
    45

    How to loop through columns

    I have the following code that matches the date between two worksheets and copies and pastes the data for a specific column. In the one below it copies and paste for F14. I would like to loop through the columns such as F14 then G14 then H14 etc. How can I go about doing that?

    Dim rFind As Range, MatchDate As Date, ms As Worksheet, ws As Worksheet, rng As Range, cel
    
    Application.ScreenUpdating = 0
    
    Set ms = Sheets("DCF Equity")
    
    MatchDate = Sheets("DCF Equity").Range("F14")
    
    
    With Sheets("Data Check")
    
    
        Set rFind = .Rows(1).Find(MatchDate, LookIn:=xlValues, LookAt:=xlWhole)
        
        If Not rFind Is Nothing Then
         
            For Each cel In ms.Range("B16", ms.Range("B" & Rows.Count).End(xlUp))
            
              Set rng = .Columns(2).Find(cel, LookIn:=xlValues, LookAt:=xlWhole)
            
                  If Not rng Is Nothing Then
                     ms.Range("F" & cel.Row) = .Cells(rng.Row, rFind.Column)
                  End If
            Next
        End If
    End With
    Set ms = Nothing
    Set ws = Nothing
    Set rFind = Nothing
    Set rng = Nothing
    Application.ScreenUpdating = True
    
    Application.ScreenUpdating = 0
    Last edited by Leith Ross; 12-29-2013 at 09:04 PM. Reason: Added Code Tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to loop through columns

    Hello todd18us,

    Dim rFind As Range, ms As Worksheet, ws As Worksheet, rng As Range, cel
    Dim cell As Range
    
    Application.ScreenUpdating = 0
    
    Set ms = Sheets("DCF Equity")
    
    For Each cell In ms.Range("F14:H14").Cells
    
    With Sheets("Data Check")
        Set rFind = .Rows(1).Find(cell.Value, LookIn:=xlValues, LookAt:=xlWhole)
        
        If Not rFind Is Nothing Then
         
            For Each cel In ms.Range("B16", ms.Range("B" & Rows.Count).End(xlUp))
            
              Set rng = .Columns(2).Find(cel, LookIn:=xlValues, LookAt:=xlWhole)
            
                  If Not rng Is Nothing Then
                     ms.Range("F" & cel.row) = .Cells(rng.row, rFind.Column)
                  End If
            Next
        End If
    End With
    
    Next cell
    
    Set ms = Nothing
    Set ws = Nothing
    Set rFind = Nothing
    Set rng = Nothing
    Application.ScreenUpdating = True
    
    Application.ScreenUpdating = 0
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    12-10-2013
    Location
    usa
    MS-Off Ver
    Excel 2013
    Posts
    45

    Re: How to loop through columns

    The code works but only copies and pastes the last column of data and puts it in column F of DCF. There are only two columns of data. So program should copy column and paste two columns and put it in column F and column G. Any ideas?

    Thank you B. Johnson

  4. #4
    Registered User
    Join Date
    12-10-2013
    Location
    usa
    MS-Off Ver
    Excel 2013
    Posts
    45

    Re: How to loop through columns

    Seem like there is a problem with ms.Range("F" & Cel.Row) = .Cells(rng.Row, rFind.Column)

    There should be a moving target for where the paste should occur. First in F then in G and so on of DCF.

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,996

    Re: How to loop through columns

    Option Explicit
    Sub example()
        Dim rFind As Range, _
        MatchDate As Date, _
        ms As Worksheet, _
        ws As Worksheet, _
        rng As Range, _
        Cel As Range
        
        Dim TestCol  As Range
        Dim FirstDCFCol As String
        Dim LastDCFCol  As String
        
        Application.ScreenUpdating = 0
        Set ms = Sheets("DCF Equity")
        
        With Sheets("DCF Equity")
    '--------------------------------------------
            ' option #1: hard coded range
            For Each TestCol In .Range("F14:H14")
    
    '------------------   OR   ---------------------
    
            'option #2: dynamic last column
            FirstDCFCol = "F14"
            LastDCFCol = .Cells(14, Columns.Count).End(xlToLeft).Address
            
            For Each TestCol In .Range(FirstDCFCol, LastDCFCol)
    '---------------------------------------------
    
                MatchDate = TestCol.Value
                
                With Sheets("Data Check")
                    Set rFind = .Rows(1).Find(MatchDate, LookIn:=xlValues, LookAt:=xlWhole)
                    If Not rFind Is Nothing Then
                        For Each Cel In ms.Range("B16", ms.Range("B" & Rows.Count).End(xlUp))
                            Set rng = .Columns(2).Find(Cel, LookIn:=xlValues, LookAt:=xlWhole)
                            If Not rng Is Nothing Then
                                ms.Range("F" & Cel.Row) = .Cells(rng.Row, rFind.Column)
                            End If
                        Next Cel
                    End If
                End With    'data ck
            Next TestCol
            
        End With    'dcf eq
        
        Set ms = Nothing
        Set ws = Nothing
        Set rFind = Nothing
        Set rng = Nothing
        Application.ScreenUpdating = True
        Application.ScreenUpdating = 0
    End Sub
    Ben Van Johnson

  6. #6
    Registered User
    Join Date
    12-10-2013
    Location
    usa
    MS-Off Ver
    Excel 2013
    Posts
    45

    Re: How to loop through columns

    Hi,

    Thank you for the revised code. However, the above code does not match the data between the two worksheets. I cannot get it to run.

    Todd

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,996

    Re: How to loop through columns

    To attach a Workbook
    (please do not post pictures of worksheets)
    • Click Advanced (next to quick post),
    • Scroll down until you see "Manage Attachments",
    • Click that then select "add files" (top right corner).
    • Click "Select Files" find your file, click "open" click "upload"
    • Once the upload is completed the file name will appear below the input boxes in this window.
    • Click "Done" at bottom right to close the Attachment Manager.
    • Click "Submit Reply"

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Loop through and sum columns
    By Hurricanefly in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 05-17-2013, 05:39 PM
  2. loop through columns in range and only copy columns that have value of 0
    By gherzberg in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-07-2012, 12:55 PM
  3. For each to loop through columns
    By alien3011 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-23-2008, 04:14 AM
  4. Loop through Columns
    By Sally in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-23-2006, 02:24 AM
  5. loop over columns
    By kizzie in forum Excel General
    Replies: 4
    Last Post: 08-10-2005, 09:05 AM

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