Results 11 to 18 of 18

Thread: VBA Macro which create new lines by codes

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #14
    Member p45cal's Avatar
    Join Date
    Oct 2013
    Posts
    94
    Rep Power
    13
    Alan, no I'm not getting notifications of responses…

    This one tries to preserve the leading zeroes (even if there are more than one) being guided by the length of the string directly before the hyphen:
    Code:
    Sub blah()
    Dim Results(), Destn As Range, rngSce As Range, Sce, j, Count, SceRw, a, itm, b, Padding, i, Cde, k
    Set Destn = Sheets("New").Range("A1")    'top left cell of where the results will go.
    Set rngSce = Sheets("Old").Range("A1").CurrentRegion
    Sce = rngSce.Value
    For j = 1 To 2    '2 loops, first time to get a count of rows needed, second time to populate array
      Count = 1
      For SceRw = 2 To UBound(Sce)
        a = Split(Application.Trim(Sce(SceRw, 3)), ";")
        For Each itm In a
          b = Split(Application.Trim(itm), "-")
          If UBound(b) > 0 Then 'there's a hyphen:
            Padding = Len(Application.Trim(b(0)))
            For i = CLng(b(0)) To CLng(b(1))
              Count = Count + 1
              If j > 1 Then
                Cde = Format(i, Application.Rept(0, Padding))
                For k = 1 To UBound(Sce, 2)
                  Results(Count, k) = Sce(SceRw, k)
                Next k
                Results(Count, 3) = Cde
              End If
            Next i
          Else 'there's no hyphen:
            Count = Count + 1
            If j > 1 Then
              Cde = Application.Trim(b(0))
              For k = 1 To UBound(Sce, 2)
                Results(Count, k) = Sce(SceRw, k)
              Next k
              Results(Count, 3) = Cde
            End If
          End If
        Next itm
      Next SceRw
      If j = 1 Then    'create new array
        ReDim Results(1 To Count, 1 To UBound(Sce, 2))
        For k = 1 To UBound(Sce, 2)    'populate top row of headers:
          Results(1, k) = Sce(1, k)
        Next k
      End If
    Next j
    Destn.Resize(UBound(Results)).Offset(, 2).NumberFormat = "@"    'format 3rd column as Text
    Destn.Resize(UBound(Results), UBound(Results, 2)).Value = Results
    End Sub
    Attached Files Attached Files

Similar Threads

  1. Replies: 14
    Last Post: 09-07-2016, 01:24 AM
  2. Replies: 9
    Last Post: 08-05-2013, 11:28 PM
  3. Replies: 0
    Last Post: 07-24-2013, 11:20 PM
  4. Replies: 3
    Last Post: 06-01-2013, 11:31 AM
  5. VBA editor auto-deletes spaces at the ends of lines
    By LalitPandey87 in forum Excel Help
    Replies: 0
    Last Post: 06-26-2012, 07:53 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
  •