Results 1 to 6 of 6

Thread: Revisiting Redistribute a Delimited Column Of Data into Separate Rows

  1. #1
    Junior Member
    Join Date
    Jun 2015
    Posts
    3
    Rep Power
    0

    Revisiting Redistribute a Delimited Column Of Data into Separate Rows

    Hi. I am trying to use your macro Redistribute a Delimited Column Of Data into Separate Rows (Keeping Other Data As Is). It is almost perfect for a task that I need to automate. Your code separates comma delimited data, and I modified it to look for hyphen delimited, which is the limit to my coding abilities. So, for example, I have data that has a range, like a page range 4-7. Your macro splits it into a row with 4 and a row with 7, keeping the other data. Is there a way to get it to create the rows within the range as well. For example for the range 4-7, could it create a row for 4, a row for 5, a row for 6, and a row for 7 (keeping the other data intact)? Better yet, is it possible to combine this code with your code for Generalized Series Expansions to create a super tool that can handle almost anything and separate them into individual rows. I have very little coding knowledge, so quite frankly, I am humbly asking if it would be possible for you to find some time to modify your code. If you need any other information, please let me know. Thanks for your time.
    Last edited by leukram; 06-16-2015 at 07:08 PM.

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    I'm sure Rick will get back to you once he gets the time. Meanwhile, did you achieve your objective with the two individual codes from Rick?
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    14
    Quote Originally Posted by leukram View Post
    Better yet, is it possible to combine this code with your code for Generalized Series Expansions to create a super tool that can handle almost anything and separate them into individual rows. I have very little coding knowledge, so quite frankly, I am humbly asking if it would be possible for you to find some time to modify your code.
    Easy enough to do, simply include the ExpandedSeries function in the same module as the RedistributeData macro and modify one line of code (highlighted in red below) in the RedistributeData macro to accommodate the call to the ExpandedSeries function...
    Code:
    Sub RedistributeData()
      Dim X As Long, LastRow As Long, A As Range, Table As Range, Data() As String
      Const Delimiter As String = ", "
      Const DelimitedColumn As String = "C"
      Const TableColumns As String = "A:C"
      Const StartRow As Long = 2
      Application.ScreenUpdating = False
      LastRow = Columns(TableColumns).Find(What:="*", SearchOrder:=xlRows, _
                SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row
      For X = LastRow To StartRow Step -1
        Data = Split(ExpandedSeries(Cells(X, DelimitedColumn), Delimiter), Delimiter)
        If UBound(Data) > 0 Then
          Intersect(Rows(X + 1), Columns(TableColumns)).Resize(UBound(Data)).Insert xlShiftDown
        End If
        If Len(Cells(X, DelimitedColumn)) Then
          Cells(X, DelimitedColumn).Resize(UBound(Data) + 1) = WorksheetFunction.Transpose(Data)
        End If
      Next
      LastRow = Cells(Rows.Count, DelimitedColumn).End(xlUp).Row
      On Error Resume Next
      Set Table = Intersect(Columns(TableColumns), Rows(StartRow).Resize(LastRow - StartRow + 1))
      If Err.Number = 0 Then
        Table.SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
        Columns(DelimitedColumn).SpecialCells(xlFormulas).Clear
        Table.Value = Table.Value
      End If
      On Error GoTo 0
      Application.ScreenUpdating = True
    End Sub
    
    Function ExpandedSeries(ByVal S As String, Optional Delimiter As String = ", ") As Variant
      Dim X As Long, Y As Long, Z As Long
      Dim Letter As String, Numbers() As String, Parts() As String
      S = Chr$(1) & Replace(Replace(Replace(Replace(Application.Trim(Replace(S, ",", _
          " ")), " -", "-"), "- ", "-"), " ", " " & Chr$(1)), "-", "-" & Chr$(1))
      Parts = Split(S)
      For X = 0 To UBound(Parts)
        If Parts(X) Like "*-*" Then
          For Z = 1 To InStr(Parts(X), "-") - 1
            If IsNumeric(Mid(Parts(X), Z, 1)) And Mid$(Parts(X), Z, 1) <> "0" Then
              Letter = Left(Parts(X), Z + (Left(Parts(X), 1) Like "[A-Za-z" & Chr$(1) & "]"))
              Exit For
            End If
          Next
          Numbers = Split(Replace(Parts(X), Letter, ""), "-")
          If Not Numbers(1) Like "*[!0-9" & Chr$(1) & "]*" Then Numbers(1) = Val(Replace(Numbers(1), Chr$(1), "0"))
          On Error GoTo SomethingIsNotRight
          For Z = Numbers(0) To Numbers(1) Step Sgn(-(Numbers(1) > Numbers(0)) - 0.5)
            ExpandedSeries = ExpandedSeries & Delimiter & Letter & Z
          Next
        Else
          ExpandedSeries = ExpandedSeries & Delimiter & Parts(X)
        End If
      Next
      ExpandedSeries = Replace(Mid(ExpandedSeries, Len(Delimiter) + 1), Chr$(1), "")
      Exit Function
    SomethingIsNotRight:
      ExpandedSeries = CVErr(xlErrValue)
    End Function
    Note: Leave the delimiters as I have them set... do not try to change them.

  4. #4
    Junior Member
    Join Date
    Jun 2015
    Posts
    3
    Rep Power
    0
    Thanks for looking at this. I pasted the code you posted into a module. When I run the macro, I get Runtime Error '13': Type mismatch. Any ideas on what is causing that? Thanks again for your time.

  5. #5
    Junior Member
    Join Date
    Jun 2015
    Posts
    3
    Rep Power
    0
    Quote Originally Posted by Excel Fox View Post
    I'm sure Rick will get back to you once he gets the time. Meanwhile, did you achieve your objective with the two individual codes from Rick?
    I did have some success, but there were a few combinations of range and comma delimited data that were not being processed.

  6. #6
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    14
    Quote Originally Posted by leukram View Post
    I did have some success, but there were a few combinations of range and comma delimited data that were not being processed.
    If you show me a few of the values that are not working for you, then I can test them out on my system and see if I can figure out what the problem is.

Similar Threads

  1. Replies: 34
    Last Post: 03-13-2015, 02:26 PM
  2. Replies: 4
    Last Post: 09-25-2014, 03:12 PM
  3. Replies: 3
    Last Post: 07-09-2014, 08:08 AM
  4. Replies: 3
    Last Post: 07-29-2013, 11:32 PM
  5. Replies: 4
    Last Post: 05-01-2013, 09:49 PM

Posting Permissions

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