+ Reply to Thread
Results 1 to 4 of 4

rearranging column data with repeat instances and blank cells

Hybrid View

  1. #1
    Registered User
    Join Date
    09-08-2016
    Location
    Los Angeles, CA
    MS-Off Ver
    Office 365
    Posts
    22

    rearranging column data with repeat instances and blank cells

    Hello wise benefactor! Thank you for lending a hand. I do hope this is the right place for this question.

    I have data to rearrange and output to two sheets. Data is in worksheet "raw" and output sheets are "output_1" and "output_2", respectively on attached.
    First, data columns 1 to 9 are characteristics of retailers, of which I've manually selected 94 unique and copied to output_1. I want to rearrange the the grades and dates in Columns 10 and 11 to output_1. Not every date has a grade, and each retailer does not exhibit the same dates. Because "raw" columns have duplicates, I have not been able to fill in the other gaps on "output_1". I hope the layout shows more clearly what I'm trying to do.
    Secondly, in output_2, I want to arrange the grade change dates and corresponding grades for each unique retailer. Can VBA do that?
    Open to all ideas. Thank you again.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    12-14-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2007
    Posts
    439

    Re: rearranging column data with repeat instances and blank cells

    Try this
    Sub Output1()
    Dim ArrData As Variant, ArrResult(1 To 200, 1 To 470), i As Long, oDic As Object, sKey As String, lIndex As Long, lPos As Long
    Dim lStartDate As Long, lDayPos As Long
    ArrData = Sheets("raw").Range("A2:L" & Sheets("raw").Cells(&H100000, 1).End(xlUp).Row).Value2
    lStartDate = Sheets("output_1").Range("K1").Value2
    Sheets("output_1").UsedRange.Offset(2).ClearContents
    Set oDic = CreateObject("Scripting.Dictionary")
    For i = 2 To UBound(ArrData, 1)
        sKey = ArrData(i, 1) & "_" & _
              ArrData(i, 2) & "_" & _
              ArrData(i, 3) & "_" & _
              ArrData(i, 4) & "_" & _
              ArrData(i, 5) & "_" & _
              ArrData(i, 6) & "_" & _
              ArrData(i, 8) & "_" & _
              ArrData(i, 9) & "_"
        If oDic.Exists(sKey) Then
            lPos = oDic.Item(sKey)
            If ArrData(i, 11) < ArrResult(lPos, 10) Then
                ArrResult(lPos, 10) = ArrData(i, 11)
            End If
        Else
            lIndex = lIndex + 1
            oDic.Add sKey, lIndex
            ArrResult(lIndex, 1) = ArrData(i, 1)
            ArrResult(lIndex, 2) = ArrData(i, 8)
            ArrResult(lIndex, 3) = ArrData(i, 9)
            ArrResult(lIndex, 4) = ArrData(i, 5)
            'ArrResult(lIndex, 5) = ArrData(i, 7)
            ArrResult(lIndex, 6) = ArrData(i, 2)
            ArrResult(lIndex, 7) = ArrData(i, 3)
            ArrResult(lIndex, 8) = ArrData(i, 4)
            ArrResult(lIndex, 9) = ArrData(i, 6)
            ArrResult(lIndex, 10) = ArrData(i, 11)
            lPos = lIndex
        End If
        lDayPos = DateDiff("m", lStartDate, ArrData(i, 11) + 1)
        ArrResult(lPos, 10 + lDayPos) = ArrData(i, 10)
    Next
    Sheets("output_1").Range("A3").Resize(lIndex, UBound(ArrResult, 2)).Value = ArrResult
    End Sub
    Sub Output2()
    Dim ArrData As Variant, ArrResult(1 To 10000, 1 To 12) As Variant, i As Long, j As Long, lIndex As Long
    Dim lStartDate As Long, lDayPos As Long
    ArrData = Sheets("output_1").Range("A1").Resize(Sheets("output_1").Cells(&H100000, 1).End(xlUp).Row, 470).Value2
    lStartDate = Sheets("output_1").Range("K1").Value2
    For i = 3 To UBound(ArrData, 1)
        lIndex = lIndex + 1
        For j = 1 To 10
            ArrResult(lIndex, j) = ArrData(i, j)
        Next
        ArrResult(lIndex, 11) = ArrResult(lIndex, 10)
        lDayPos = DateDiff("m", lStartDate, ArrResult(lIndex, 11) + 1)
        ArrResult(lIndex, 12) = ArrData(i, 10 + lDayPos)
        For j = 10 + lDayPos + 1 To 470
            If ArrData(i, j) <> ArrData(i, j - 1) Then
                lIndex = lIndex + 1
                ArrResult(lIndex, 11) = ArrData(1, j)
                ArrResult(lIndex, 12) = ArrData(i, j)
            End If
        Next
    Next
    Sheets("output_2").Range("B2").Resize(lIndex, 12).Value = ArrResult
    End Sub
    Note: Output_2 is rearranged from data of output_1. So, allway run output_1 before run output_2.

  3. #3
    Registered User
    Join Date
    09-08-2016
    Location
    Los Angeles, CA
    MS-Off Ver
    Office 365
    Posts
    22

    Re: rearranging column data with repeat instances and blank cells

    Thank you so much huuthang_bd! Figuring out how you did it, and some NOOB errors on my part, prolonged this thank you message. Your solution has breathed new life into my work!

  4. #4
    Valued Forum Contributor
    Join Date
    12-14-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2007
    Posts
    439

    Re: rearranging column data with repeat instances and blank cells

    Thank you for reply. Please mark this thread as SOLVED if your problem was solved.

+ 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. How to create pivot table from set with repeat data, blank cells
    By PT_AZ in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 12-14-2018, 07:59 PM
  2. [SOLVED] Count Instances of Repeat and Non-Repeat Dates
    By mgs in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-08-2015, 09:54 PM
  3. [SOLVED] Need VBA Macro to Copy and paste column data in blank rows and repeat through spreadsheet
    By slk1186 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-07-2014, 12:50 PM
  4. Return ALL instances of non-blank cells
    By SAsplin in forum Excel General
    Replies: 2
    Last Post: 03-22-2013, 06:03 AM
  5. Merge data from cells on 2nd columns in which the cells of 1st column repeat
    By jagke in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-14-2013, 01:06 PM
  6. rearranging cells in a column
    By kras in forum Excel General
    Replies: 2
    Last Post: 11-13-2008, 04:01 AM
  7. Repeat data in blank cells
    By mackey in forum Excel General
    Replies: 2
    Last Post: 02-06-2008, 09:18 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