+ Reply to Thread
Results 1 to 9 of 9

a double loop?

Hybrid View

jimmy1981 a double loop? 08-03-2010, 09:50 AM
royUK Re: a double loop? 08-03-2010, 09:52 AM
royUK Re: a double loop? 08-03-2010, 09:54 AM
jimmy1981 Re: a double loop? 08-03-2010, 09:56 AM
royUK Re: a double loop? 08-03-2010, 09:59 AM
jimmy1981 Re: a double loop? 08-03-2010, 10:04 AM
Andrew-R Re: a double loop? 08-03-2010, 10:23 AM
jimmy1981 Re: a double loop? 08-03-2010, 10:41 AM
Andrew-R Re: a double loop? 08-03-2010, 10:48 AM
  1. #1
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    a double loop?

    Hi,

    I have a sheet and i want a macro to go down column A until it finds a name then go right until it finds the name of the column.

    so on the sheet attached, it would go down a and find Test3 then go along till the column header was example 4 and return the figure 0.7323.

    ive got it going down, and then going across but cant get it stop accordin to the column header:
    Sub test()
    
    Do
    
        If ActiveCell.Value = "test3" Then
    
    Do
            ActiveCell.Offset(0, 1).Select
        
        Loop Until ActiveCell.Value = "example4"
            
        End If
    
        ActiveCell.Offset(1, 0).Select
    
        Loop Until IsEmpty(ActiveCell.Offset(0, 1))
    
    
    
    
    End Sub
    Attached Files Attached Files
    Last edited by royUK; 08-03-2010 at 09:53 AM. Reason: add code tags

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: a double loop?

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: a double loop?

    Code tags added this time

    Don't use a Loop, use .Find - it will be much quicker

    can you attach a dummy workbook with some data in so that we can see what you are attempting
    Last edited by royUK; 08-03-2010 at 09:57 AM.

  4. #4
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: a double loop?

    How will that work?

    I have 1500 lines of data where i would want it to find the value so presumed it would be quicker to use a loop. Never used find before though.

    Edit: sorry for not doing the code properly.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: a double loop?

    Loops should be avoided, they are not fast.

    Please attach some dummy data & what you want to find

  6. #6
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: a double loop?

    dummy attached.

    as you can see it looks for test2 then for example 25 and shows the figure between the 2.

    i just cant figure how to get the loop to stop when the column header equals what its searching for.

    but now you say use find i'm not sure at all!

    cheers
    Attached Files Attached Files
    Last edited by jimmy1981; 08-03-2010 at 10:18 AM.

  7. #7
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: a double loop?

    This function will allow you to return the cross reference of a row title and column header:

    Function CrossReference(RowTitle, ColTitle, DataRange As Range) As Variant
    Dim FindRow As Range, FindCol As Range
    
    Set FindRow = DataRange.Columns(1).Find(RowTitle, lookat:=xlWhole)
    Set FindCol = DataRange.Rows(1).Find(ColTitle, lookat:=xlWhole)
    
    If FindRow Is Nothing Or FindCol Is Nothing Then
      CrossReference = ""
    Else
      CrossReference = DataRange.Cells(FindRow.Row, FindCol.Column).Value
    End If
    
    End Function
    And here's a simple example sub calling it:

    Sub Test()
    
    MsgBox CrossReference("test3", "example4", ThisWorkbook.Sheets(1).Cells)
    
    End Sub

  8. #8
    Registered User
    Join Date
    08-03-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: a double loop?

    ok, i'm really not following this find. think i'm out of my league a bit here.

    can it not be done with a loop, i know you say it might take longer but not too bothered about that so long as i can understand it.

  9. #9
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: a double loop?

    It can't be done in *a* loop, but it can be done in 2 While loops ...

    Sub Loopy()
    
    Dim RowLoop, ColLoop
    
    RowLoop = 1
    While ThisWorkbook.Sheets(1).Cells(RowLoop, 1).Value <> "Test3"
      RowLoop = RowLoop + 1
    Wend
    
    ColLoop = 1
    While ThisWorkbook.Sheets(1).Cells(1, ColLoop).Value <> "Example4"
      ColLoop = ColLoop + 1
    Wend
    
    MsgBox ThisWorkbook.Sheets(1).Cells(RowLoop, ColLoop).Value
    
    End Sub
    However, you'd be far better reading up on how the .find method works and using that, it's many times faster and much easier to error trap and control.

+ 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