+ Reply to Thread
Results 1 to 13 of 13

Row Duplication based on Cell <>"" in Clumn +1 = TRUE

Hybrid View

  1. #1
    Registered User
    Join Date
    09-22-2011
    Location
    san diego
    MS-Off Ver
    Excel 2013
    Posts
    76

    Row Duplication based on Cell <>"" in Clumn +1 = TRUE

    Attached picture much better description than I can write. Looking to get data into a pivot friendly format via a vba loop. I don't have much experience with writing loops. Many thanks in advance!

    2018-01-22 09_08_45-Example.xlsx - Saved.png

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Row Duplication based on Cell <>"" in Clumn +1 = TRUE

    Try this for Results starting "A2".
    Sub MG22Jan29
    Dim Rng As Range, Dn As Range
    Dim AcRng As Range, Num As Long, n As Long, Ac As Long, c As Long, Ray(), aNum As Long
    Set Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
        For Each Dn In Rng
            Num = Range("H" & Dn.Row, Cells(Dn.Row, Columns.Count).End(xlToLeft)).Count
            aNum = aNum + Num
            ReDim Preserve Ray(1 To 8, 1 To aNum)
            For n = 1 To Num
                 c = c + 1
                For Ac = 1 To 8
                   If Ac = 8 Then
                        Ray(Ac, c) = n
                   Else
                        Ray(Ac, c) = Dn(, Ac)
                   End If
                Next Ac
            Next n
        Next Dn
    Range("A2").Resize(c, 8) = Application.Transpose(Ray)
    End Sub
    Regards Mick

  3. #3
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Row Duplication based on Cell <>"" in Clumn +1 = TRUE

    Other way

    Sub test()
    Dim col_target As Long, lr As Long, x As Long, i As Long, qtrow As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    With Range("1:1")
        col_target = .Find("Target").Column
        last_col = .Find("*", searchdirection:=xlPrevious).Column
    End With
    For x = lr To 2 Step -1
        qtrow = Cells(x, Columns.Count).End(xlToLeft).Value - 1
        If qtrow > 0 Then
            Range("A" & x + 1).Resize(qtrow).EntireRow.Insert
            Range("A" & x).Resize(, col_target).Copy Destination:=Range("A" & x + 1, "A" & x + qtrow)
            For i = 1 To qtrow
                Cells(x + i, col_target) = i + 1
            Next
        End If
    Next
    Range("A:A").Offset(, col_target).Resize(, last_col).ClearContents
    End Sub
    Kind regards
    Leo

  4. #4
    Registered User
    Join Date
    09-22-2011
    Location
    san diego
    MS-Off Ver
    Excel 2013
    Posts
    76

    Re: Row Duplication based on Cell <>"" in Clumn +1 = TRUE

    Sorry about bringing this back, I thought I had it , but i'm getting a type mismatch error highlighting
    qtrow = Cells(x, Columns.Count).End(xlToLeft).Value - 1
    when trying to run Leo's code. I'm sure it's my bad...the values in the target columns are text fields

  5. #5
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,468

    Re: Row Duplication based on Cell <>"" in Clumn +1 = TRUE

    I am sorri to distrub how if i want save this treats to my favorit or to my list thank

  6. #6
    Registered User
    Join Date
    09-22-2011
    Location
    san diego
    MS-Off Ver
    Excel 2013
    Posts
    76

    Re: Row Duplication based on Cell <>"" in Clumn +1 = TRUE

    AWESOME thank you both a TON!

  7. #7
    Registered User
    Join Date
    09-22-2011
    Location
    san diego
    MS-Off Ver
    Excel 2013
    Posts
    76

    Re: Row Duplication based on Cell <>"" in Clumn +1 = TRUE

    Looks like both are set up for integers. MickG, I would love to understand how Ray works. Looks awesome. and I can't find any definitions for it online

  8. #8
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,468

    Re: Row Duplication based on Cell <>"" in Clumn +1 = TRUE

    Sub test()
    Dim arr(), i&, ii&, c&, n, t&, j&, a
    With [a2].CurrentRegion: a = .Value
    For i = 1 To UBound(a, 1)
         n = .Rows(i).Columns.End(xlToRight): t = t + n
         ReDim Preserve arr(1 To 8, 1 To t)
         For j = 1 To n: rw = rw + 1
             For ii = 1 To 8
                If ii = 8 Then
                     arr(ii, rw) = j
                Else
                     arr(ii, rw) = a(i, ii)
                End If
             Next ii
         Next j
    Next i
    End With
    [a2].Resize(t, 8).Value = Application.Transpose(arr)
    End Sub]

  9. #9
    Registered User
    Join Date
    09-22-2011
    Location
    san diego
    MS-Off Ver
    Excel 2013
    Posts
    76

    Re: Row Duplication based on Cell <>"" in Clumn +1 = TRUE

    Quote Originally Posted by daboho View Post
    Sub test()
    Dim arr(), i&, ii&, c&, n, t&, j&, a
    With [a2].CurrentRegion: a = .Value
    For i = 1 To UBound(a, 1)
         n = .Rows(i).Columns.End(xlToRight): t = t + n
         ReDim Preserve arr(1 To 8, 1 To t)
         For j = 1 To n: rw = rw + 1
             For ii = 1 To 8
                If ii = 8 Then
                     arr(ii, rw) = j
                Else
                     arr(ii, rw) = a(i, ii)
                End If
             Next ii
         Next j
    Next i
    End With
    [a2].Resize(t, 8).Value = Application.Transpose(arr)
    End Sub]
    Still getting a type mismatch

  10. #10
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Row Duplication based on Cell <>"" in Clumn +1 = TRUE

    without file we only can guess

    Kind regards
    Leo

  11. #11
    Registered User
    Join Date
    09-22-2011
    Location
    san diego
    MS-Off Ver
    Excel 2013
    Posts
    76

    Re: Row Duplication based on Cell <>"" in Clumn +1 = TRUE

    Quote Originally Posted by LeoTaxi View Post
    without file we only can guess

    Kind regards
    Leo
    Sorry about that. The attachment is exactly the data types and structure of the real. Stuff without headers extends out anywhere from a single column to column cc and rows continue down for several thousand.

    Thanks so much in advance, sorry about bad presentation of the situation.
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Row Duplication based on Cell <>"" in Clumn +1 = TRUE

    Try

    Sub test()
    Dim col_t As Long, lr As Long, lr2 As Long, x As Long, i As Long, ii As Long, j As Long
    Dim arr() As Variant, arr2() As Variant
    With Sheets("Sheet1")
        lr = .Range("A" & Rows.Count).End(xlUp).Row
        With .Range("1:1")
            col_t = .Find("Target").Column - 1
        End With
        .Range("A1").Resize(, col_t + 1).Copy .Range("A" & Rows.Count).End(xlUp).Offset(1)
        For x = 2 To lr
            With .Rows(x)
                l_col = .Find("*", searchdirection:=xlPrevious).Column
                ec = l_col - col_t
            End With
            ReDim arr(1 To ec, 1 To col_t)
            For i = 1 To ec
                For j = 1 To col_t
                    arr(i, j) = Cells(x, j)
                Next
            Next
            ReDim arr2(1 To ec, 1 To 1)
            For i = col_t + 1 To l_col
                ii = ii + 1
                arr2(ii, 1) = Cells(x, i)
            Next
            lr2 = .Range("A" & Rows.Count).End(xlUp).Row + 1
            .Range("A" & lr2).Resize(UBound(arr), UBound(arr, 2)) = arr
            .Range("A" & lr2).Offset(, col_t).Resize(ec) = arr2
            ii = 0
        Next
        .Range("A2", "A" & lr).EntireRow.Delete
        .Range("A1").EntireRow.Delete
    End With
    End Sub
    Kind regards
    Leo
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    09-22-2011
    Location
    san diego
    MS-Off Ver
    Excel 2013
    Posts
    76

    Re: Row Duplication based on Cell <>"" in Clumn +1 = TRUE

    That worked great! Thank you. Only thing that i needed to do was separate all rows that only have one target (again my bad, bad example file) and then it worked like a charm!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. "Fun times in no mans land" Translation - "Selective Duplication"
    By amonty87 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-09-2013, 03:21 AM
  2. Replies: 6
    Last Post: 09-25-2013, 01:51 PM
  3. [SOLVED] Test for blank cell should be "True" but "False" action is taken.
    By Aceso in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 02-09-2013, 05:47 PM
  4. Replies: 5
    Last Post: 10-12-2010, 06:46 AM
  5. Replies: 7
    Last Post: 05-13-2006, 05:02 PM
  6. If "text" exist within "cell" then TRUE
    By JemyM in forum Excel - New Users/Basics
    Replies: 7
    Last Post: 09-16-2005, 07:41 PM
  7. [SOLVED] IF(VLOOKUP("MYDATA", MYNAME, 4) = 0, "TRUE", "FALSE")
    By Souris in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-17-2005, 01:05 AM
  8. [SOLVED] set "value if true" to "fill cell with color"
    By Feeta in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-23-2005, 04:05 AM

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