VBA - Grab Unique Values and Paste them x number of times

stwp86

New Member
Joined
Jun 28, 2012
Messages
19
Hi Everyone . . .

Not really an Excel VBA expert, so I thought I would come try my luck here. Essentially, what I am trying to accomplish is this:

I have two columns of data that I would like to generate, a date range and an hour range. I have the following range of data in Sheet 1 Column B:[TABLE="class: grid, width: 250, align: center"]
<tbody>[TR]
[TD]Date[/TD]
[/TR]
[TR]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]11/13/2013[/TD]
[/TR]
[TR]
[TD]11/12/2013[/TD]
[/TR]
[TR]
[TD]11/14/2013[/TD]
[/TR]
[TR]
[TD]11/14/2013[/TD]
[/TR]
[TR]
[TD]11/12/2013[/TD]
[/TR]
[TR]
[TD]11/15/2013[/TD]
[/TR]
[TR]
[TD]11/11/2013[/TD]
[/TR]
</tbody>[/TABLE]

What I need to do is extract the unique values, and then paste them in Sheet 2 Column B 24 times each. In Column A of Sheet 2, I would like to Paste 0-23 (a set of 0-23 for every unique date). This is the desired output:
[TABLE="class: grid, width: 250, align: center"]
<tbody>[TR]
[TD="align: center"]Hour[/TD]
[TD="align: center"]Date[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]13[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]14[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]16[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]17[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]18[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]19[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]20[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]21[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]22[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]23[/TD]
[TD]11/11/2013[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]11/12/2013[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]11/12/2013[/TD]
[/TR]
[TR]
[TD]...[/TD]
[TD]...[/TD]
[/TR]
</tbody>[/TABLE]


Any thoughts on how I can make this happen?

Thanks for the help in advance!

Travis
 
hi,

Here is a non-formula & non-code approach. Give the dates data (incl header) a normal defined name, say "Dates". Make a list called "Hours" with header Hours & liast 0, 1, 2, 3, etc

Now make a query table with SQL: "SELECT DISTINCT Dates.Date, Hours.Hour FROM Dates, Hours"

regards
 
Upvote 0
hi,

Here is a non-formula & non-code approach. Give the dates data (incl header) a normal defined name, say "Dates". Make a list called "Hours" with header Hours & liast 0, 1, 2, 3, etc

Now make a query table with SQL: "SELECT DISTINCT Dates.Date, Hours.Hour FROM Dates, Hours"

regards

Thanks, but that is not quite what I am trying to do here. . .

this is what I have thus far:

Dim uniqueDates As Integer
Dim Lastrow As Long
Lastrow = Range("B" & Rows.Count).End(xlUp).Row



uniqueDates = Evaluate("=SUM(IF(FREQUENCY(MATCH(Sheet1!$B$2:$B$" & Lastrow & ",Sheet1!$B$2:$B$" & Lastrow & ",0),MATCH(Sheet1!$B$2:$B$" & Lastrow & ",Sheet1!$B$2:$B$" & Lastrow & ",0))>0,1))")
Sheet2.Activate

Dim numberOfDates As Integer
Dim numberOfHours As Integer
Dim numberOfRows As Integer

numberOfRows = 2
numberOfDates = 1

Do While numberOfDates <= uniqueDates
Do While numberOfHours < 23
Range("A" & numberOfRows).Value = numberOfHours
numberOfHours = numberOfHours + 1
numberOfRows = numberOfRows + 1
Loop
numberOfDates = numberOfDates + 1
Loop


But it seems to only write out 0-23 1 time instead of looping through uniqueDates which should be 5 times
 
Upvote 0
Hi..

Try this..

Code:
Private Sub CommandButton1_Click()
Dim vArr, i As Long
Sheets("Sheet1").Cells(2, 2).Resize(Range("B" & Rows.Count).End(xlUp).Row - 1, 1).Sort [B1], 1
With CreateObject("scripting.dictionary")
            For Each it In Sheets("Sheet1").Columns(2).SpecialCells(2).Offset(1)
                x0 = .Item(it.Value)
            Next
            Z = .keys
        End With
     vArr = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24)
    For i = LBound(Z) To UBound(Z) - 1
    Sheets("Sheet2").Range("B" & Rows.Count).End(xlUp).Offset(1).Resize(24, 1) = Z(i)
    Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(24, 1) = Application.Transpose(vArr)
    Next i
End Sub

<b>Sheet1</b><br /><br /><table border="1" cellspacing="0" cellpadding="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:102px;" /><col style="width:157px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >A</td><td >B</td></tr><tr style="height:20px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td > </td><td style="color:#333333; font-weight:bold; font-family:Verdana; font-size:10pt; ">Date</td></tr><tr style="height:20px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td > </td><td style="color:#333333; font-family:Verdana; font-size:10pt; text-align:left; ">11/11/2013</td></tr><tr style="height:20px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td > </td><td style="color:#333333; font-family:Verdana; font-size:10pt; text-align:left; ">11/11/2013</td></tr><tr style="height:33px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td > </td><td style="color:#333333; font-family:Verdana; font-size:10pt; text-align:left; ">11/13/2013</td></tr><tr style="height:20px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td > </td><td style="color:#333333; font-family:Verdana; font-size:10pt; text-align:left; ">11/12/2013</td></tr><tr style="height:33px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td > </td><td style="color:#333333; font-family:Verdana; font-size:10pt; text-align:left; ">11/14/2013</td></tr><tr style="height:33px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td > </td><td style="color:#333333; font-family:Verdana; font-size:10pt; text-align:left; ">11/14/2013</td></tr><tr style="height:20px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td > </td><td style="color:#333333; font-family:Verdana; font-size:10pt; text-align:left; ">11/12/2013</td></tr><tr style="height:33px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td > </td><td style="color:#333333; font-family:Verdana; font-size:10pt; text-align:left; ">11/15/2013</td></tr><tr style="height:20px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td > </td><td style="color:#333333; font-family:Verdana; font-size:10pt; text-align:left; ">11/11/2013</td></tr></table> <br />Excel tables to the web - Excel Jeanie Html 4

<b>Sheet2</b><br /><br /><table border="1" cellspacing="0" cellpadding="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:64px;" /><col style="width:114px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >A</td><td >B</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td >Hour</td><td >Date</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td style="text-align:right; ">0</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td style="text-align:right; ">1</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td style="text-align:right; ">2</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td style="text-align:right; ">3</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td style="text-align:right; ">4</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td style="text-align:right; ">5</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td style="text-align:right; ">6</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td style="text-align:right; ">7</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td style="text-align:right; ">8</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >11</td><td style="text-align:right; ">9</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >12</td><td style="text-align:right; ">10</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >13</td><td style="text-align:right; ">11</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >14</td><td style="text-align:right; ">12</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >15</td><td style="text-align:right; ">13</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >16</td><td style="text-align:right; ">14</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >17</td><td style="text-align:right; ">15</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >18</td><td style="text-align:right; ">16</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >19</td><td style="text-align:right; ">17</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >20</td><td style="text-align:right; ">18</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >21</td><td style="text-align:right; ">19</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >22</td><td style="text-align:right; ">20</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >23</td><td style="text-align:right; ">21</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >24</td><td style="text-align:right; ">22</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >25</td><td style="text-align:right; ">23</td><td style="text-align:right; ">11/11/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >26</td><td style="text-align:right; ">0</td><td style="text-align:right; ">11/12/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >27</td><td style="text-align:right; ">1</td><td style="text-align:right; ">11/12/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >28</td><td style="text-align:right; ">2</td><td style="text-align:right; ">11/12/2013</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >29</td><td style="text-align:right; ">3</td><td style="text-align:right; ">11/12/2013</td></tr></table> <br />Excel tables to the web - Excel Jeanie Html 4
 
Upvote 0
Hi..

Try this..

Code:
Private Sub CommandButton1_Click()
Dim vArr, i As Long
Sheets("Sheet1").Cells(2, 2).Resize(Range("B" & Rows.Count).End(xlUp).Row - 1, 1).Sort [B1], 1
With CreateObject("scripting.dictionary")
            For Each it In Sheets("Sheet1").Columns(2).SpecialCells(2).Offset(1)
                x0 = .Item(it.Value)
            Next
            Z = .keys
        End With
     vArr = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24)
    For i = LBound(Z) To UBound(Z) - 1
    Sheets("Sheet2").Range("B" & Rows.Count).End(xlUp).Offset(1).Resize(24, 1) = Z(i)
    Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(24, 1) = Application.Transpose(vArr)
    Next i
End Sub

Sheet1

*AB
*Date
*
*
*
*
*
*
*
*
*

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:102px;"><col style="width:157px;"></colgroup><tbody>
[TD="bgcolor: #cacaca, align: center"]1[/TD]

[TD="bgcolor: #cacaca, align: center"]2[/TD]

[TD="align: left"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]3[/TD]

[TD="align: left"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]4[/TD]

[TD="align: left"]11/13/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]5[/TD]

[TD="align: left"]11/12/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]6[/TD]

[TD="align: left"]11/14/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]7[/TD]

[TD="align: left"]11/14/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]8[/TD]

[TD="align: left"]11/12/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]9[/TD]

[TD="align: left"]11/15/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]10[/TD]

[TD="align: left"]11/11/2013[/TD]

</tbody>

Excel tables to the web - Excel Jeanie Html 4

Sheet2

*AB
HourDate

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:64px;"><col style="width:114px;"></colgroup><tbody>
[TD="bgcolor: #cacaca, align: center"]1[/TD]

[TD="bgcolor: #cacaca, align: center"]2[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]3[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]4[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]5[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]6[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]7[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]8[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]9[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]10[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]11[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]12[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]13[/TD]
[TD="align: right"]11[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]14[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]15[/TD]
[TD="align: right"]13[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]16[/TD]
[TD="align: right"]14[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]17[/TD]
[TD="align: right"]15[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]18[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]19[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]20[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]21[/TD]
[TD="align: right"]19[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]22[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]23[/TD]
[TD="align: right"]21[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]24[/TD]
[TD="align: right"]22[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]25[/TD]
[TD="align: right"]23[/TD]
[TD="align: right"]11/11/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]26[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]11/12/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]27[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]11/12/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]28[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]11/12/2013[/TD]

[TD="bgcolor: #cacaca, align: center"]29[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]11/12/2013[/TD]

</tbody>

Excel tables to the web - Excel Jeanie Html 4


Perfect!

Thanks for the help . . .

Travis
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top