0

Actual work to loop through the Column A which has same value of with certain range, with that range have to check the latest date with the comment in Column B and print the comment with date in Column C kindly help me to find the solution for this problem.. Or Guide to find the solution for this problem... Kindly check the Screenshot for clear information.. Thanks in Advance Experts

enter image description here

Community
  • 1
  • 1
Bharath Babu
  • 65
  • 2
  • 8
  • Please provide any code you have tried thusfar. StackOverflow is here to collaborate, not code-for-you. If you have not tried any code thusfar, please try the Macro Recorder on the Developer tab. If you are just looking for tips, look into Application.Max() for the most recent date (Assumes formatting correctly). – Cyril Sep 25 '17 at 19:43
  • @Cyril Let me post the code now.. But my problem how to get the Application.Max() in a certain Range.. – Bharath Babu Sep 25 '17 at 19:46

2 Answers2

5

Loops are probably the most powerful things is all computer programming topics. Please see the examples below for some ideas of how to achieve your goals.

For Each cell in a range data for each example

One of the most common things you will do when programming VBA in Excel is looping though a collection of cells in a specified range, as in the example below which prints the Address and Value of 4 cells on the 'Data' worksheet to the immediate window: $B$2:a, $C$2:b, $B$3:1, $C$3:2.

Dim rng As Range: Set rng = Application.Range("Data!B2:C3")
Dim cel As Range
For Each cel In rng.Cells
    With cel
        Debug.Print .Address & ":" & .Value
    End With
Next cel

Loop through the cells in a row data for each example

The code below shows how to loop through the cells in the row with RowIndex:=2. Applied to the data in the sheet on the right this will return 1, 2. From this we see that rows are counted from the starting point of rng, so the row is 3 on the worksheet, 2 inside rng. Also, only cells inside the set range rng are taken.

Dim rng As Range: Set rng = Application.Range("Data!B2:C3")
Dim i As Integer
For i = 1 To rng.Rows.Count
    Debug.Print rng.Cells(RowIndex:=2, ColumnIndex:=i).Value
Next

Loop through the cells in a column

The code below shows how to loop through the cells in the column with ColumnIndex:=B. Applied to the data in the sheet on the right this will return a, 1, 2. From this we see that columns are counted from the starting point of rng, so the column is C on the worksheet, B inside rng. Also, only cells inside the set range rng are taken.

Dim rng As Range: Set rng = 
Dim i As Integer
For i = 1 To rng.Rows.Count
    Debug.Print rng.Cells(RowIndex:=i, ColumnIndex:="B").Value
Next

Loop through the columns in a range

The code below shows how to loop through the columns in the Range B2:C4. Applied to the data in the sheet on the right this will return 2, 3. From this we see that columns are counted from the starting point of the worksheet.

Dim rng As Range: Set rng = Application.Range("B2:C4")
Dim col As Range
For Each col In rng.Columns
    Debug.Print col.Column
Next col

Loop through the rows in a range

The code below shows how to loop through the rows in the Range B2:C4. Applied to the data in the sheet on the right this will return 2, 3, 4. From this we see that rows are counted from the starting point of the worksheet.

Dim rng As Range: Set rng = Application.Range("B2:C4")
Dim col As Range
For Each col In rng.Rows
    Debug.Print col.Row
Next col

Loop through the areas in a range data for each 2 areas example

Often we assume a range to have a rectangular shape, but this need not be the case. The example sheet on the right shows a selection containing two areas: Selection.Address returns $B$2:$C$3,$F$2:$F$3. Such a situation may also occur as a result of the Intersect method, or other causes. To handle the two ranges separately can can pick then from the Areas collection:

Dim rng As Range: Set rng = Application.Selection
Dim rngArea As Range
For Each rngArea In rng.Areas
    Debug.Print rngArea.Address
Next rngArea
ASH
  • 20,759
  • 19
  • 87
  • 200
2

I would recommend declaring some dimensions in a fairly simple approach (assumes you have sorted Column A):

Dim i As Long, j As Long, k As Long, LR As Long
LR = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To LR
    If Cells(i, 1).Value = Cells(i - 1).Value Then
        If j = 0 Then
           j = Cells(i - 1, 1).Row
        End If
    Else
        If j > 0 Then
            k = Cells(i - 1, 1).Row
            Cells(j, 3).Value = Application.Max(Range(Cells(j, 1), Cells(k, 1)))
            j = Cells(i, 1).Row
            k = 0
        End If
    End If
Next i
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Cyril
  • 6,448
  • 1
  • 18
  • 31
  • Not particularly a fan of coding for someone that hasn't put in the code and shown some additional effort, but given the comment about how to use multiple ranges within the loop, you really need to show the code to have it make sense. – Cyril Sep 25 '17 at 20:06
  • Actually Bro... I've taken both columns in to loops and compared the values to get the latest date..I got little bit confused in that .. so that only i've posted like this.. – Bharath Babu Sep 25 '17 at 20:21
  • @BharathBabu If you can, try posting up those loops; though, I feel like this posted code should get you to where you need want to be. Per your comment on the post itself, you mentioned just having issues with using the Max() with smaller, more specific ranges, so that is what I have posted. – Cyril Sep 25 '17 at 20:44
  • @Cyril `LR` should be `Long` not `String`! Why do you declare `j` and `k` as string and then don't use it at all but you don't declare `a` and `b`? **Always use `Option Explicit`** – Pᴇʜ Sep 26 '17 at 06:24
  • @Peh Was thinking a and b, and my goto is always i, j, k when typing; I copied from another sub and that's my bad. Updated to show appropriately. – Cyril Sep 26 '17 at 12:34
  • 1
    @Cyril Eehm but `j` and `k` should be `Long` too. I hope you are familiar about the difference between `Long` and `String`. String counters don't make much sense. – Pᴇʜ Sep 26 '17 at 12:38
  • 1
    @Peh I am familiar and only noted that LR was to be updated, rather than looking through and being more thorough in updating the rest. My apologies. I will say that i've been a lot better about using Long over Integer since you posted on something I did a few weeks back. – Cyril Sep 26 '17 at 12:40