+ Reply to Thread
Results 1 to 8 of 8

getting the first and last value out of a non continious data set

Hybrid View

  1. #1
    Registered User
    Join Date
    03-22-2009
    Location
    wichita, kansas
    MS-Off Ver
    Excel 2007
    Posts
    15

    getting the first and last value out of a non continious data set

    imagine you have column A

    1
    3
    5

    7
    6
    1
    8


    6
    9
    8

    i want the numbers 1,5,7,8,6,8 to be printed into column b. make sense?

    i am taking the starting and ending values of a non continuous data set. so far i have tried using a for next loop but i can only get the first value. i'm jsut stuck. ANY help is welcomed!

    thanks in advance mike.
    Last edited by withy; 03-23-2009 at 03:43 AM.

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

    Re: getting the first and last value out of a non continious data set

    Welcome to the Board.

    There are a few options open to you, one:

    Public Sub FilterData()
    Dim rngArea As Range
    For Each rngArea In Columns(1).SpecialCells(xlCellTypeConstants, xlNumbers).Areas
        Cells(Rows.Count, "B").End(xlUp).Offset(1).Value = rngArea.Cells(1, 1).Value
        Cells(Rows.Count, "B").End(xlUp).Offset(1).Value = rngArea.Cells(rngArea.Rows.Count, 1).Value
    Next rngArea
    End Sub
    The above assumes your values in Column A are numeric and not result of formulae, if they are result of formulae you will need to adjust the SpeicalCells line to xlCellTypeFormulas as opposed to xlCellTypeConstants

  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: getting the first and last value out of a non continious data set

    If your data starts in A1, try this
    Option Explicit
    
    Sub x()
        Dim rng    As Range
        Dim cl     As Range
        Dim Rw     As Long
    
        Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
        Set rng = rng.Resize(rng.Rows.Count + 1)
        Rw = 2
        Cells(1, 2).Value = Cells(1, 1).Value
        For Each cl In rng
            If IsEmpty(cl) Then
                Cells(Rw, 2).Value = cl.Offset(-1, 0).Value
                Cells(Rw + 1, 2).Value = cl.Offset(1, 0).Value
                Rw = Rw + 2
            End If
        Next cl
    End Sub
    Copy the code
    Select the workbook in which you want to store the code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste
    Hope that helps.

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

    Free DataBaseForm example

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

    Re: getting the first and last value out of a non continious data set

    Roy, I don't think that would work if you had variable blanks between range areas (as implied in sample data set) - I think using SpecialCells with Areas is probably a safer / quicker method.

  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: getting the first and last value out of a non continious data set

    I looked at your example & it would be better than mine, I never thought of using areas nor noticed the variable blanks.

  6. #6
    Registered User
    Join Date
    03-22-2009
    Location
    wichita, kansas
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: getting the first and last value out of a non continious data set

    beautiful. just beautiful. marked as solved.

    also, i'm jsut curious..... but i've been around forums for a long time and they all have a gen chat.... no gen chat here?

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

    Re: getting the first and last value out of a non continious data set

    There's the Water Cooler which you can join

  8. #8
    Registered User
    Join Date
    03-22-2009
    Location
    wichita, kansas
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: getting the first and last value out of a non continious data set

    HA HA, yea that makes sense..... even says off topic chat! guess i should probably get to sleep cause my brain is MELTING. thanks again guys.

+ 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