Results 1 to 9 of 9

Thread: Select Column and Identify First and Last non blank cell value per segment (Range between Blanks)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Sep 2017
    Posts
    5
    Rep Power
    0
    Mr. Rothstein, thank you for your assistance I appreciate it! Would you be able to provide a brief description of what each line does or break down the script. Only, if you have the time. Again, thank you this worked perfect for my needs and I hope it helps others!

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by J.Suarez View Post
    Mr. Rothstein, thank you for your assistance I appreciate it! Would you be able to provide a brief description of what each line does or break down the script. Only, if you have the time. Again, thank you this worked perfect for my needs and I hope it helps others!
    See inline comments in red...
    Code:
    Sub SegmentsDown()
      ' I always declare my variables by name and type
      Dim RowNum As Long, Ar As Range
      ' Starts a loop that looks down Column A and that will iterate
      ' through each contiguous range of cells (called an Area) that
      ' contain a constant value.
      For Each Ar In Columns("A").SpecialCells(xlConstants).Areas
        ' RowNum is the row number that the currently iterated Area
        ' will be written to after it is formed into the hyphen delimited
        ' range that you want.
        RowNum = RowNum + 1
        ' The cell in Column B where the output will go is formatted as
        ' Text... this stops Excel from interpreting a number dash number
        ' as a date value (which is what it tends to do if the month number
        ' position is under 13
        Cells(RowNum, "B").NumberFormat = "@"
        ' The Area referenced by the Ar variable contains the values in the
        ' cells... this line of code pulls out the first value and the last value
        ' in the Area and puts a dash between them and then writes that
        ' value into the currently iterated RowNumber row in Column B.
        Cells(RowNum, "B").Value = Ar(1) & "-" & Ar(Ar.Count)
      Next
    End Sub
    
    Sub SegmentsAcross()
      ' I always declare my variables by name and type
      Dim Col As Long, Ar As Range
      ' We are going to iterate the columns in the loop that follows
      ' and since the first output column is Column B (the 2nd column)
      ' we seed the Col variable with the number 1 so that when we add
      ' one to it on the first iteration o f the loop, it increases to 2 to
      ' match the first output column.
      Col = 1
      ' Starts a loop that looks across Row 1 and that will iterate
      ' through each contiguous range of cells (called an Area) that
      ' contain a constant value.
      For Each Ar In Columns("A").SpecialCells(xlConstants).Areas
        ' Increase the column reference variable by one column
        Col = Col + 1
        ' This is just a simple range copy... the values in the current Area
        ' being iterated are already in a single column, so we simply copy
        ' to the range starting with the cell in Row 1 at the column specified
        ' in the Col variable.
        Ar.Copy Cells(1, Col)
      Next
    End Sub

  3. #3
    Junior Member
    Join Date
    Sep 2017
    Posts
    5
    Rep Power
    0
    Awesome, thanks Sir! I appreciate your fine efforts!

Similar Threads

  1. VBA Code To Run Through A Column And Email Blanks
    By hooper12345 in forum Excel Help
    Replies: 1
    Last Post: 02-08-2016, 10:25 AM
  2. Replies: 3
    Last Post: 02-20-2015, 07:03 PM
  3. Replies: 0
    Last Post: 12-24-2013, 01:36 PM
  4. Replies: 2
    Last Post: 09-24-2012, 11:19 PM
  5. Formula To Identify A Cell Has Duplicate Words
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 07-03-2011, 05:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •