Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
EK EK is offline
external usenet poster
 
Posts: 20
Default Populate Data In Rows

I have following data:

A B
-----------------
Apples A
Pears D
Orange C
Grapes B

If column B is "A", repeat corresponding column A data 4 times in a row. If
column B is "B", repeat data 3 times in a row, and so forth. Please help with
solution which will result in the following:

column C
----------
Apples
Apples
Apples
Apples
Grapes
Grapes
Grapes
Orange
Orange
Pears

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,202
Default Populate Data In Rows

You can do what you asked with this macro...

Sub CreateRepeats()
Dim X As Long
Dim Y As Long
Dim LastRow As Long
Dim LastDataRow As Long
LastDataRow = 1
With Worksheets("Sheet1")
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
For X = 1 To LastRow
For Y = 1 To 69 - Asc(.Cells(X, 2).Value)
Cells(LastDataRow, 3).Value = Cells(X, 1)
LastDataRow = LastDataRow + 1
Next
Next
.Range("C1").Sort Key1:=.Columns("C")
End With
End Sub

It wasn't clear to me what criteria you used to sort Column C... it's either
alphabetical or by the repeat count... I assumed alphabetical in the above
routine.

Rick


"EK" wrote in message
...
I have following data:

A B
-----------------
Apples A
Pears D
Orange C
Grapes B

If column B is "A", repeat corresponding column A data 4 times in a row.
If
column B is "B", repeat data 3 times in a row, and so forth. Please help
with
solution which will result in the following:

column C
----------
Apples
Apples
Apples
Apples
Grapes
Grapes
Grapes
Orange
Orange
Pears


  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default Populate Data In Rows

First sort your data by column B and then run:

Sub temp()
n = Cells(Rows.Count, 1).End(xlUp).Row
k = 1
For nn = 1 To n
nt = 69 - Asc(Cells(nn, 2).Value)
v = Cells(nn, 1).Value
For times = 1 To nt
Cells(k, 3).Value = v
k = k + 1
Next
Next
End Sub

--
Gary''s Student - gsnu200750


"EK" wrote:

I have following data:

A B
-----------------
Apples A
Pears D
Orange C
Grapes B

If column B is "A", repeat corresponding column A data 4 times in a row. If
column B is "B", repeat data 3 times in a row, and so forth. Please help with
solution which will result in the following:

column C
----------
Apples
Apples
Apples
Apples
Grapes
Grapes
Grapes
Orange
Orange
Pears

  #4   Report Post  
Posted to microsoft.public.excel.misc
EK EK is offline
external usenet poster
 
Posts: 20
Default Populate Data In Rows

Sorry, I'm afraid I do not know macro. Do I have to substitute the X, Y,
LastRow, etc. with specific numbers? What do they correspond to? -EK

"Rick Rothstein (MVP - VB)" wrote:

You can do what you asked with this macro...

Sub CreateRepeats()
Dim X As Long
Dim Y As Long
Dim LastRow As Long
Dim LastDataRow As Long
LastDataRow = 1
With Worksheets("Sheet1")
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
For X = 1 To LastRow
For Y = 1 To 69 - Asc(.Cells(X, 2).Value)
Cells(LastDataRow, 3).Value = Cells(X, 1)
LastDataRow = LastDataRow + 1
Next
Next
.Range("C1").Sort Key1:=.Columns("C")
End With
End Sub

It wasn't clear to me what criteria you used to sort Column C... it's either
alphabetical or by the repeat count... I assumed alphabetical in the above
routine.

Rick


"EK" wrote in message
...
I have following data:

A B
-----------------
Apples A
Pears D
Orange C
Grapes B

If column B is "A", repeat corresponding column A data 4 times in a row.
If
column B is "B", repeat data 3 times in a row, and so forth. Please help
with
solution which will result in the following:

column C
----------
Apples
Apples
Apples
Apples
Grapes
Grapes
Grapes
Orange
Orange
Pears



  #5   Report Post  
Posted to microsoft.public.excel.misc
EK EK is offline
external usenet poster
 
Posts: 20
Default Populate Data In Rows

I think I got the answer from this macro. Appreciate all who tried to the
help. Thanks!

"Gary''s Student" wrote:

First sort your data by column B and then run:

Sub temp()
n = Cells(Rows.Count, 1).End(xlUp).Row
k = 1
For nn = 1 To n
nt = 69 - Asc(Cells(nn, 2).Value)
v = Cells(nn, 1).Value
For times = 1 To nt
Cells(k, 3).Value = v
k = k + 1
Next
Next
End Sub

--
Gary''s Student - gsnu200750


"EK" wrote:

I have following data:

A B
-----------------
Apples A
Pears D
Orange C
Grapes B

If column B is "A", repeat corresponding column A data 4 times in a row. If
column B is "B", repeat data 3 times in a row, and so forth. Please help with
solution which will result in the following:

column C
----------
Apples
Apples
Apples
Apples
Grapes
Grapes
Grapes
Orange
Orange
Pears



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,202
Default Populate Data In Rows

I've made a minor modification to my subroutine, so when you follow the
instructions below, make sure you use the code posted in this message.

Go to the worksheet with your data on it, right-click the tab for this sheet
and select View Code from the popup menu that appears. This process will
have taken you into the VBA editor and opened the code window for the
worksheet you were just on. The next thing for you to do is Copy the code
below my signature and Paste it into the opened code window in the VBA
editor. You can now close the VBA editor and return to your worksheet. To
use this macro from the worksheet, simple press Alt+F8 and Select/Run the
CreateReports macro from the list.

Rick

Sub CreateRepeats()
Dim X As Long
Dim Y As Long
Dim LastRow As Long
Dim LastDataRow As Long
LastDataRow = 1
With ActiveSheet
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
For X = 1 To LastRow
For Y = 1 To 69 - Asc(.Cells(X, 2).Value)
Cells(LastDataRow, 3).Value = Cells(X, 1)
LastDataRow = LastDataRow + 1
Next
Next
.Range("C1").Sort Key1:=.Columns("C")
End With
End Sub

  #7   Report Post  
Posted to microsoft.public.excel.misc
EK EK is offline
external usenet poster
 
Posts: 20
Default Populate Data In Rows

One other thing. From this macro, what do I need to change in order to
re-populate with a different definition such as:
A=6 times, B=3 times, C=2 times and D=1 time.

EK
"EK" wrote:

I think I got the answer from this macro. Appreciate all who tried to the
help. Thanks!

"Gary''s Student" wrote:

First sort your data by column B and then run:

Sub temp()
n = Cells(Rows.Count, 1).End(xlUp).Row
k = 1
For nn = 1 To n
nt = 69 - Asc(Cells(nn, 2).Value)
v = Cells(nn, 1).Value
For times = 1 To nt
Cells(k, 3).Value = v
k = k + 1
Next
Next
End Sub

--
Gary''s Student - gsnu200750


"EK" wrote:

I have following data:

A B
-----------------
Apples A
Pears D
Orange C
Grapes B

If column B is "A", repeat corresponding column A data 4 times in a row. If
column B is "B", repeat data 3 times in a row, and so forth. Please help with
solution which will result in the following:

column C
----------
Apples
Apples
Apples
Apples
Grapes
Grapes
Grapes
Orange
Orange
Pears

  #8   Report Post  
Posted to microsoft.public.excel.misc
EK EK is offline
external usenet poster
 
Posts: 20
Default Populate Data In Rows

Yes, it works. Except that this code also re-sorted the order of the data in
Col A and B. I would like it sorted Col C by the repeat count and leave Col A
and B intact. The code from Gary"s Student did just that.

For this code, what do I have to change in order to re-populate with a
different definition such as:
A=6 times, B=3 times, C=2 times and D=1 time?

Thanks, EK.




"Rick Rothstein (MVP - VB)" wrote:

I've made a minor modification to my subroutine, so when you follow the
instructions below, make sure you use the code posted in this message.

Go to the worksheet with your data on it, right-click the tab for this sheet
and select View Code from the popup menu that appears. This process will
have taken you into the VBA editor and opened the code window for the
worksheet you were just on. The next thing for you to do is Copy the code
below my signature and Paste it into the opened code window in the VBA
editor. You can now close the VBA editor and return to your worksheet. To
use this macro from the worksheet, simple press Alt+F8 and Select/Run the
CreateReports macro from the list.

Rick

Sub CreateRepeats()
Dim X As Long
Dim Y As Long
Dim LastRow As Long
Dim LastDataRow As Long
LastDataRow = 1
With ActiveSheet
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
For X = 1 To LastRow
For Y = 1 To 69 - Asc(.Cells(X, 2).Value)
Cells(LastDataRow, 3).Value = Cells(X, 1)
LastDataRow = LastDataRow + 1
Next
Next
.Range("C1").Sort Key1:=.Columns("C")
End With
End Sub


  #9   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,202
Default Populate Data In Rows

Yes, it works. Except that this code also re-sorted the order of the data
in
Col A and B. I would like it sorted Col C by the repeat count and leave
Col A
and B intact. The code from Gary"s Student did just that.


Fixed in the code below.

For this code, what do I have to change in order to re-populate with a
different definition such as:
A=6 times, B=3 times, C=2 times and D=1 time?


You didn't say you would need other encodings, so I (and Gary) created an
algorithm to do exactly what you asked. To make it changeable will require a
different algorithmic approach. Assuming you will need these repeat values
to be changeable, I think the best way to handle it would be to store the
repeat values in cells on the spreadsheet (you can hide the Column so they
are not seen if you wish). For the (new) subroutine code below, I am
assuming the A repeat value will be stored in F1, the B repeat value in F2,
the C repeat value in F3 and the D repeat value in F4.

Sub CreateRepeats()
Dim X As Long
Dim Y As Long
Dim LastRow As Long
Dim LastDataRow As Long
LastDataRow = 1
With ActiveSheet
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
For X = 1 To LastRow
For Y = 1 To Range("F" & CStr(Asc(.Cells(X, 2).Value) - 64)).Value
Cells(LastDataRow, 3).Value = Cells(X, 1)
LastDataRow = LastDataRow + 1
Next
Next
.Range("C:C").Sort Key1:=.Columns("C")
End With
End Sub


Rick

  #10   Report Post  
Posted to microsoft.public.excel.misc
EK EK is offline
external usenet poster
 
Posts: 20
Default Populate Data In Rows

Rick and Gary,
I apologise for not being specific earlier. Thank you for the new code.

EK

"Rick Rothstein (MVP - VB)" wrote:

Yes, it works. Except that this code also re-sorted the order of the data
in
Col A and B. I would like it sorted Col C by the repeat count and leave
Col A
and B intact. The code from Gary"s Student did just that.


Fixed in the code below.

For this code, what do I have to change in order to re-populate with a
different definition such as:
A=6 times, B=3 times, C=2 times and D=1 time?


You didn't say you would need other encodings, so I (and Gary) created an
algorithm to do exactly what you asked. To make it changeable will require a
different algorithmic approach. Assuming you will need these repeat values
to be changeable, I think the best way to handle it would be to store the
repeat values in cells on the spreadsheet (you can hide the Column so they
are not seen if you wish). For the (new) subroutine code below, I am
assuming the A repeat value will be stored in F1, the B repeat value in F2,
the C repeat value in F3 and the D repeat value in F4.

Sub CreateRepeats()
Dim X As Long
Dim Y As Long
Dim LastRow As Long
Dim LastDataRow As Long
LastDataRow = 1
With ActiveSheet
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
For X = 1 To LastRow
For Y = 1 To Range("F" & CStr(Asc(.Cells(X, 2).Value) - 64)).Value
Cells(LastDataRow, 3).Value = Cells(X, 1)
LastDataRow = LastDataRow + 1
Next
Next
.Range("C:C").Sort Key1:=.Columns("C")
End With
End Sub


Rick


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
enter a value and have excel populate a table with that many rows Dave F Excel Discussion (Misc queries) 1 September 18th 06 02:06 PM
Lift Data from Rows to Populate HTML Variables [email protected] Excel Discussion (Misc queries) 1 August 1st 06 01:30 PM
can a dde link be used to populate excel creating new rows? jpk3535 Excel Discussion (Misc queries) 0 July 25th 06 08:04 PM
Populate Rows from worksheet Names. Trever B Excel Discussion (Misc queries) 2 May 15th 06 05:08 AM
How do I import external data populate rows instead of columns. KWE39 Excel Discussion (Misc queries) 0 July 1st 05 09:53 PM


All times are GMT +1. The time now is 04:07 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"