+ Reply to Thread
Results 1 to 16 of 16

Macro Loop to Extract Specific Values and Min/Max Values From Column/Rows Range

Hybrid View

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

    Re: Macro Loop to Extract Specific Values and Min/Max Values From Column/Rows Range

    Thanks for the PM, my pleasure. Try this. I'm still not 100% sure if the interval is 20 or 20+1. If this code doesn't do the job it would be helpful if you could add to your attachment the expected results based on your example.
    Sub x()
    
    Dim rng As Range, rStart As Range, nRow As Long, r As Long
    
    Sheet1.Activate
    Set rStart = Application.InputBox("Enter start cell", Type:=8)
    If rStart.Count > 1 Or rStart Is Nothing Then Exit Sub
    nRow = Application.InputBox("How many rows", Type:=1)
    If nRow = 0 Then Exit Sub
    
    Set rng = rStart.Resize(nRow, 5)
    Do Until IsEmpty(rStart)
        With Sheet2.Cells(Rows.Count, 1).End(xlUp)(2)
            .Value = rStart
            .Offset(, 1).Value = rStart.Offset(, 1)
            .Offset(, 2).Value = WorksheetFunction.Min(rng.Columns(3))
            .Offset(, 3).Value = WorksheetFunction.Max(rng.Columns(4))
            .Offset(, 4).Value = rStart.Offset(, 4)
        End With
        Set rng = rng.Offset(nRow)
        Set rStart = rStart.Offset(nRow)
    Loop
    
    End Sub

  2. #2
    Forum Contributor
    Join Date
    06-03-2009
    Location
    Florida
    MS-Off Ver
    Excel 2007 / Excel 2010
    Posts
    106

    Re: Macro Loop to Extract Specific Values and Min/Max Values From Column/Rows Range

    The first 2 columns looks like we have the right values. The other 3 don't seem to be working correctly. I've attached a sample spreadsheet. This assumes we have rows of 12 beginning in A2. Sheet 2 is the macro values and Sheet 3 are desired values.
    Attached Files Attached Files

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

    Re: Macro Loop to Extract Specific Values and Min/Max Values From Column/Rows Range

    This works for your example. That has max in C and min in D whereas your original post said the reverse so I have gone with the example.
    Sub x()
    
    Dim rng As Range, rStart As Range, nRow As Long, r As Long
    
    Sheet1.Activate
    Set rStart = Application.InputBox("Enter start cell", Type:=8)   'Range("A2")
    If rStart.Count > 1 Or rStart Is Nothing Then Exit Sub
    nRow = Application.InputBox("How many rows", Type:=1)   '12
    If nRow = 0 Then Exit Sub
    
    Set rng = rStart.Resize(nRow, 5)
    Do Until IsEmpty(rStart)
        With Sheet2.Cells(Rows.Count, 1).End(xlUp)(2)
            .Value = rStart
            .Offset(, 1).Value = rStart.Offset(, 1)
            .Offset(, 2).Value = WorksheetFunction.Max(rng.Columns(3))
            .Offset(, 3).Value = WorksheetFunction.Min(rng.Columns(4))
            .Offset(, 4).Value = rStart.Offset(nRow - 1, 4)
        End With
        Set rng = rng.Offset(nRow)
        Set rStart = rStart.Offset(nRow)
    Loop
    
    End Sub
    Last edited by StephenR; 06-03-2009 at 06:19 PM.

  4. #4
    Forum Contributor
    Join Date
    06-03-2009
    Location
    Florida
    MS-Off Ver
    Excel 2007 / Excel 2010
    Posts
    106

    Re: Macro Loop to Extract Specific Values and Min/Max Values From Column/Rows Range

    I'm sorry. You're right. Thank you very much! I'll now study this code so I can learn it.

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

    Re: Macro Loop to Extract Specific Values and Min/Max Values From Column/Rows Range

    No problem. Once you've had a chance to check, if the code does what you want please could you mark the thread as Solved?

+ 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