+ Reply to Thread
Results 1 to 10 of 10

Need help building a loop

Hybrid View

  1. #1
    Registered User
    Join Date
    05-28-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    45

    Need help building a loop

    I need a loop that goes across columns, when it hits a cell E.G 'Team 1' the loop then goes down to a particular row E.G row 11 and fills in team member names that it gets from a list on a different sheet, once it has filled in all of 'Team 1' members, it goes back to moving across columns until it hits the next cell with E.G 'Team 2' ...

    Anyone able to provide some code or point me in the right way of a decent tutorial page?

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Need help building a loop

    look at for next loops, and offset.

    if you could attach a sample, I could illustrate.

  3. #3
    Registered User
    Join Date
    05-28-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Need help building a loop

    I've attached an example of what i am dealing with

    Thanks
    Attached Files Attached Files
    Last edited by flindy87; 06-04-2013 at 05:45 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Need help building a loop

    
    Dim rngRowToCheck As Excel.Range
    Dim rngTeamDetails As Excel.Range
    Dim strTeamLookFor As String
    Dim rw As Excel.Range
    Dim cell As Excel.Range
    Dim intPresentationRowNumber As Integer
    Dim wsTeamSheet As Excel.Worksheet
    Dim wsLookup As Excel.Worksheet
    
    Set wsTeamSheet = ThisWorkbook.Worksheets("Team Sheet")
    Set wsLookup = ThisWorkbook.Worksheets("Lookup")
    
    Set rngRowToCheck = wsTeamSheet.Rows("3:3")
    Set rngTeamDetails = wsLookup.Range("c1:e35")
    
    For Each cell In rngRowToCheck.Cells
    
        If LCase(cell.Value) Like "team*" Then
            
            intPresentationRowNumber = 10
            strTeamToLookFor = LCase(cell.Value)
            
            For Each rw In rngTeamDetails.Rows
                If LCase(rw.Cells(, 3).Value) = strTeamToLookFor Then
                    wsTeamSheet.Cells(intPresentationRowNumber, cell.Column + 1).Value = rw.Cells(, 2).Value
                    intPresentationRowNumber = intPresentationRowNumber + 1
                End If
            Next rw
        
        End If
        
    
    Next cell

  5. #5
    Registered User
    Join Date
    05-28-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Need help building a loop

    Thanks very much,

    I gave the code a test, it seems to be pulling through the Position, rather than the Full name?

  6. #6
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Need help building a loop

    I thought that was a number

    wsTeamSheet.Cells(intPresentationRowNumber, cell.Column ).Value = rw.Cells(, 1).Value
    wsTeamSheet.Cells(intPresentationRowNumber, cell.Column + 1).Value = rw.Cells(, 2).Value

  7. #7
    Registered User
    Join Date
    05-28-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Need help building a loop

    Really appreciate all your help, however i may have butchered the code a bit too much, it doesn't seem to work at all now - basically runs through the loop doing nothing. A specific line does not seem to be doing anything at all...."For Each Cell in rngRowToCheck.Cells"

    Sub Looptester()
    
    'Dim rngRowToCheck As Integer
    'Dim rngTeamDetails As Integer
    'Dim strTeamLookFor As String
    'Dim rw As Integer
    'Dim cell As Integer
    'Dim intPresentationRowNumber As Integer
    'Dim wsTeamSheet As String
    'Dim wsLookup As String
    
    
    
    Set wsTeamSheet = ThisWorkbook.Worksheets("Team Sheet")
    Set wsLookup = ThisWorkbook.Worksheets("Lookup")
    
    Set rngRowToCheck = wsTeamSheet.Rows("3:3")
    Set rngTeamDetails = wsLookup.Range("c2:e35")
    
    For Each cell In rngRowToCheck.Cells
    
        If LCase(cell.Value) Like "Team*" Then
            
            intPresentationRowNumber = 10
            strTeamToLookFor = LCase(cell.Value)
            
            For Each rw In rngTeamDetails.Rows
                If LCase(rw.Cells(, 3).Value) = strTeamToLookFor Then
                    wsTeamSheet.Cells(intPresentationRowNumber, cell.Column).Value = rw.Cells(, 1).Value
                    wsTeamSheet.Cells(intPresentationRowNumber, cell.Column + 1).Value = rw.Cells(, 2).Value
                    intPresentationRowNumber = intPresentationRowNumber + 1
                End If
            Next rw
        
        End If
        
    
    Next cell
    
    End Sub

  8. #8
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Need help building a loop

    its cause you've change the "team*" to be "Team*" LCASE changes to lower case, therefore this will never be true, leave the t as lower case.

    uncomment the dim statements

    if at the top of the code you say Option Compare Text, then there is no need for the Lcase bit.

  9. #9
    Registered User
    Join Date
    05-28-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Need help building a loop

    Ok that has fixed part of the problem, the code now runs and correctly fills out team 1 with the correct team members, however the code then stops with a "Mismatch error" and does not fill out any other team.

    Dim rngRowToCheck As Excel.Range
    Dim rngTeamDetails As Excel.Range
    Dim strTeamLookFor As String
    Dim rw As Excel.Range
    Dim cell As Excel.Range
    Dim intPresentationRowNumber As Integer
    Dim wsTeamSheet As Excel.Worksheet
    Dim wsLookup As Excel.Worksheet
    
    Dim lastRow As Long
        lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    Set wsTeamSheet = ThisWorkbook.Worksheets("Team Sheet")
    Set wsLookup = ThisWorkbook.Worksheets("Lookup")
    
    Set rngRowToCheck = wsTeamSheet.Rows("3:3")
    Set rngTeamDetails = wsLookup.Range("t2:v" & lastRow)
    
    For Each cell In rngRowToCheck.Cells
    
        If LCase(cell.Value) Like "team*" Then
            
            intPresentationRowNumber = 10
            strTeamToLookFor = LCase(cell.Value)
            
            For Each rw In rngTeamDetails.Rows
                If LCase(rw.Cells(, 3).Value) = strTeamToLookFor Then
                    wsTeamSheet.Cells(intPresentationRowNumber, cell.Column).Value = rw.Cells(, 1).Value
                    wsTeamSheet.Cells(intPresentationRowNumber, cell.Column + 1).Value = rw.Cells(, 2).Value
                    intPresentationRowNumber = intPresentationRowNumber + 1
                End If
            Next rw
        
        End If
        
    
    Next cell

  10. #10
    Registered User
    Join Date
    05-28-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Need help building a loop

    Anyone able to help?

+ 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