Automating Data Population

hitdave85

New Member
Joined
Jun 11, 2013
Messages
30
Hello,

I am trying to populate some data by relying on a table I have named Area_Table. Here is what the table looks like:

Unique IDArea 1Area 2Area 3Area 4Area 5
1001YNNNY
1002YNNNN
1003NYYYN
1004YYYYN
1005YYYYN

<tbody>
</tbody>

Here is what I am trying to populate by using the Area_Table:

Unique IDAreaAge
1001Area 120
1001Area 121
1001Area 122
1001Area 123
1001Area 124
1001Area 125
1001Area 520
1001Area 521
1001Area 522
1001Area 523
1001Area 524
1001Area 525
1002Area 120
1002Area 121
1002Area 122
1002Area 123
1002Area 124
1002Area 125
1003Area 220
1003Area 221
1003Area 222
1003Area 223
1003Area 224
1003Area 225

<tbody>
</tbody>

As you can see, for each unique ID in the Area_Table, I want to populate ages 20-25 for all of the 'Y' areas. The 'N' areas are NOT populated. The hope is that a simple click of a button will populate the data for me. I assume since I am relying on a table, that the range will become dynamic, which will allow me to add/remove unique IDs pretty easily. I'm hoping a simple macro can address this problem since I have a lot of data to populate. Any ideas?

Thanks so much for the help!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try this macro. Change the sheet names to suit your needs. The result will be in Sheet2.
Code:
Sub Test()
    Application.ScreenUpdating = False
    Dim LastRow As Long, area As Range, lCol As Long, x As Long
    lCol = Cells(1, Columns.Count).End(xlToLeft).Column
    LastRow = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = 2 To LastRow
        For Each area In Sheets("Sheet1").Range(Sheets("Sheet1").Cells(x, 2), Sheets("Sheet1").Cells(x, lCol))
            If area = "Y" Then
                With Sheets("Sheet2")
                    .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(6) = Sheets("Sheet1").Cells(x, 1)
                    .Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0).Resize(6) = Sheets("Sheet1").Cells(1, area.Column)
                    With .Cells(.Rows.Count, "C").End(xlUp).Offset(1, 0)
                        .Value = 20
                        .AutoFill .Resize(6, 1), xlFillSeries
                    End With
                End With
            End If
        Next area
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Very cool! What if I want to populate that third column with the following ages (mix of integers and strings):

0-14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64+

<colgroup><col style="mso-width-source:userset;mso-width-alt:3986;width:82pt" width="109"> </colgroup><tbody>
</tbody>

Thanks again for the help!!!
 
Upvote 0
Could you post a sample of what the end result will look like?
 
Upvote 0
1001Area 10-14
1001Area 115
1001Area 116
1001Area 117
1001Area 118
1001Area 119
1001Area 120
1001Area 121
1001Area 122
1001Area 123
1001Area 124
1001Area 125
1001Area 126
1001Area 127
1001Area 128
1001Area 129
1001Area 130
1001Area 131
1001Area 132
1001Area 133
1001Area 134
1001Area 135
1001Area 136
1001Area 137
1001Area 138
1001Area 139
1001Area 140
1001Area 141
1001Area 142
1001Area 143
1001Area 144
1001Area 145
1001Area 146
1001Area 147
1001Area 148
1001Area 149
1001Area 150
1001Area 151
1001Area 152
1001Area 153
1001Area 154
1001Area 155
1001Area 156
1001Area 157
1001Area 158
1001Area 159
1001Area 160
1001Area 161
1001Area 162
1001Area 163
1001Area 164 and over
1001Area 50-14
1001Area 515
1001Area 516
1001Area 517
1001Area 518
1001Area 519
1001Area 520
1001Area 521
1001Area 522
1001Area 523
1001Area 524
1001Area 525
1001Area 526
1001Area 527
1001Area 528
1001Area 529
1001Area 530
1001Area 531
1001Area 532
1001Area 533
1001Area 534
1001Area 535
1001Area 536
1001Area 537
1001Area 538
1001Area 539
1001Area 540
1001Area 541
1001Area 542
1001Area 543
1001Area 544
1001Area 545
1001Area 546
1001Area 547
1001Area 548
1001Area 549
1001Area 550
1001Area 551
1001Area 552
1001Area 553
1001Area 554
1001Area 555
1001Area 556
1001Area 557
1001Area 558
1001Area 559
1001Area 560
1001Area 561
1001Area 562
1001Area 563
1001Area 564 and over
1002Area 10-14
1002Area 115
1002Area 116
1002Area 117
1002Area 118
1002Area 119
1002Area 120
1002Area 121
1002Area 122
1002Area 123
1002Area 124
1002Area 125
1002Area 126
1002Area 127
1002Area 128
1002Area 129
1002Area 130
1002Area 131
1002Area 132
1002Area 133
1002Area 134
1002Area 135
1002Area 136
1002Area 137
1002Area 138
1002Area 139
1002Area 140
1002Area 141
1002Area 142
1002Area 143
1002Area 144
1002Area 145
1002Area 146
1002Area 147
1002Area 148
1002Area 149
1002Area 150
1002Area 151
1002Area 152
1002Area 153
1002Area 154
1002Area 155
1002Area 156
1002Area 157
1002Area 158
1002Area 159
1002Area 160
1002Area 161
1002Area 162
1002Area 163
1002Area 164 and over

<colgroup><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
Try:
Code:
Sub Test()
    Application.ScreenUpdating = False
    Dim LastRow As Long, LastRow2 As Long, area As Range, lCol As Long, x As Long
    LastRow2 = 15
    lCol = Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
    LastRow = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = 2 To LastRow
        For Each area In Sheets("Sheet1").Range(Sheets("Sheet1").Cells(x, 2), Sheets("Sheet1").Cells(x, lCol))
            If area = "Y" Then
                With Sheets("Sheet2")
                    .Cells(LastRow2, 1).Resize(51) = Sheets("Sheet1").Cells(x, 1)
                    .Cells(LastRow2, 2).Resize(51) = Sheets("Sheet1").Cells(1, area.Column)
                    .Cells(LastRow2, 3) = "0-14"
                    With .Cells(LastRow2 + 1, 3)
                        .Value = 15
                        .AutoFill .Resize(49, 1), xlFillSeries
                    End With
                    .Cells(.Rows.Count, "C").End(xlUp).Offset(1, 0) = "64 and over"
                    LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
                End With
            End If
        Next area
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I tried to adapt your code by using a named range, but nothing seems to happen when I run the following code. Any thoughts?

Sub Test()
Application.ScreenUpdating = False
Dim LastRow As Long, LastRow2 As Long, area As Range, lCol As Long, x As Long
LastRow2 = 15
lCol = Sheets("Inputs").Range("Plan_IDs").Columns.Count
LastRow = Sheets("Inputs").Range("Plan_IDs").Rows.Count
For x = 2 To LastRow
For Each area In Sheets("Inputs").Range("Plan_IDs").Range(Sheets("Inputs").Range("Plan_IDs").Cells(x, 15), Sheets("Inputs").Range("Plan_IDs").Cells(x, lCol))
If area = "Y" Then
With Worksheets("Rate Development")
.Cells(LastRow2, 1).Resize(51) = Sheets("Inputs").Range("Plan_IDs").Cells(x, 1)
.Cells(LastRow2, 2).Resize(51) = Sheets("Inputs").Range("Plan_IDs").Cells(1, area.Column)
.Cells(LastRow2, 3) = "0-14"
With .Cells(LastRow2 + 1, 3)
.Value = 15
.AutoFill .Resize(49, 1), xlFillSeries
End With
.Cells(.Rows.Count, "C").End(xlUp).Offset(1, 0) = "64 and over"
LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
End With
End If
Next area
Next x
Application.ScreenUpdating = True
End Sub
 
Upvote 0
The macro loops through each row of your data so a named range won't work because you would need a different named range for each row. A named range would be OK if you were looping through only one range but in your case you would need a different named range for each Unique ID.
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,854
Members
449,096
Latest member
Erald

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