VBA Help - Loop Thru List and Copy Value to a Range and repeat to next list value

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hello All,

I have a project where I need to populate a list of Movie Titles within a defined range of values that describe a $spend category. This exercise is done weekly so trying to create a script that could build my data quickly.I mocked up a small sample that is similar to what I am looking for.

In this sample, you can see the Title field is blank and the Defined Variable Field is duplicated in 3 seperate Blocks. What I am trying to achieve is the piece of code that would take a look at my list on sheet lookups and grab the first Title and paste it into cells B2:B6 and then loop this process for the next title and do this until all titles have been populated.

Book1
ABC
1RowTitleDefined Variable
21Photo
32Marketing
43Publicity
54Media
65Events
71Photo
82Marketing
93Publicity
104Media
115Events
121Photo
132Marketing
143Publicity
154Media
165Events
Sheet1


Book1
A
1List of Titles
2Harry Potter
3Speed
4Lion King
5Aliens 2
Lookups


The End Result should look like this

Book1
ABC
1RowTitleDefined Variable
21Harry PotterPhoto
32Harry PotterMarketing
43Harry PotterPublicity
54Harry PotterMedia
65Harry PotterEvents
71SpeedPhoto
82SpeedMarketing
93SpeedPublicity
104SpeedMedia
115SpeedEvents
121Lion KingPhoto
132Lion KingMarketing
143Lion KingPublicity
154Lion KingMedia
165Lion KingEvents
171Aliens 2Photo
182Aliens 2Marketing
193Aliens 2Publicity
204Aliens 2Media
215Aliens 2Events
Sheet1


Any help on this is appreciated!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi. Please, try this code.
VBA Code:
Sub RepeatData()
 Dim movie As Range, k As Long
  With Sheets("Sheet1")
   .[A2:C6].Copy .[A7].Resize((Application.CountA(Sheets("Lookups").[A:A]) - 2) * 5, 3)
   For Each movie In Sheets("Lookups").Range("A2:A" & Sheets("Lookups").Cells(Rows.Count, 1).End(3).Row)
    .Cells(2 + k, 2).Resize(5) = movie.Value: k = k + 5
   Next movie
  End With
End Sub
 
Upvote 0
Hello.
The following procedure only requires the 'Lookups' sheet:

a) Take the data from its column A and
b) Create a new sheet with the data structure you requested.

VBA Code:
Sub Macro8()
Dim a, b, R&, i&, j%
a = Range("'Lookups'!A1").CurrentRegion
ReDim b(1 To 1 + 5 * (UBound(a) - 1), 1 To 3): R = 1
b(1, 1) = "Row": b(1, 2) = "Title": b(1, 3) = "Defined"
For i = 2 To UBound(a)
  For j = 1 To 5
    R = 1 + R: b(R, 1) = j: b(R, 2) = a(i, 1)
    b(R, 3) = Array("Photo", "Marketing", "Publicity", "Media", "Events")(j - 1)
  Next
Next
With Worksheets.Add(ActiveSheet).[a1].Resize(R, 3)
  .Parent.Cells.Font.Size = 12: .Parent.Rows.RowHeight = 20: .Value = b
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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