+ Reply to Thread
Results 1 to 5 of 5

Refer to cell using for each

Hybrid View

  1. #1
    Registered User
    Join Date
    02-09-2007
    Posts
    29

    Refer to cell using for each

    Hi I have this code
    Dim Count As Integer
    For Each c1 In Worksheets("Sheet2").Range("A11:A24").Cells
        Count = 0
        For Each c2 In Worksheets("Sheet1").Range("C1:C200").Cells
            If (c1.Value = c2.Value) Then
                Count = Count + 1
            End If
        Next
        c1.Value = Count
    Next
    But what I want is a change in the last line

    c1.Value = Count
    to something like
    c1+NextRowOver.Value = Count
    Essentially, I want to use the cell in the next row over from c1. How can I do this?

    Thank you.
    Last edited by Lazhal; 02-23-2010 at 12:03 PM.

  2. #2
    Valued Forum Contributor JeanRage's Avatar
    Join Date
    03-02-2009
    Location
    Nice, France
    MS-Off Ver
    Excel 2003
    Posts
    705

    Re: Refer to cell using for each

    Hi,
    c1.Offset(1,0).Value = Count
    HTH

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Refer to cell using for each

    I think by "next row over" you mean next column - ie B11:B24

    It would be a lot quicker to replace your entire code with something like:

    With Worksheets("Sheet2").Range("A11:A24").Offset(,1)
        .FormulaR1C1 = "=COUNTIF(Sheet1!R1C3:R200C3,RC[-1])"
        .Value = .Value
    End With
    always better to work with cells in blocks than iterate.

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Refer to cell using for each

    c1.Offset(, 1).Value = Count
    But why not just use COUNTIF formula?

  5. #5
    Registered User
    Join Date
    02-09-2007
    Posts
    29

    Re: Refer to cell using for each

    great!

    thanks folks!

+ 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