Assigning weights to certain types of records from a list of weights without just copying and pasting

JulianJohannesen

New Member
Joined
May 28, 2015
Messages
1
Hello all,

I have about 29,000 records with all sorts of demographic data for individuals. These records each belong to one of 13 states or counties, 5 ethnic categories and 3 age categories. I want to assign a specific weight to each record in a category. I have my list of 13x5x3 = 195 weights. How can can assign those weights to my records without performing 195 copy and paste operations and then copying that weight factor down for the number of records in each of those categories?

A unique weight goes with each category
StateRaceAgeWeight
AZasian18-29weight1
AZasian30-59weight2
AZasian60+weight3
AZblack18-29weight4
AZblack30-59weight5
AZblack60+weight6
AZcaucasian18-29weight7
AZcaucasian30-59weight8
AZcaucasian60+weight9
… etc
Actual data without weight included
StateRaceAgeWeight
AZasian18-29
AZasian18-29
AZasian18-29
AZasian18-29
AZasian18-29
AZasian18-29
AZasian30-59
AZasian30-60
AZasian30-61
AZasian30-62
AZasian30-63
AZasian30-64
AZasian30-65
AZasian30-66
… and so on for thousands of records


<tbody>
</tbody>
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Pivot Tables were created for this purpose, I believe.
 
Upvote 0
Hi Julian,

This code should do what you would like. I tested it on a small set of data and it works. There is work you are going to have to do to complete it. I tried to include comments to explain what I was doing. If you would like to PM me feel free.
The code also assumes the data is set up exactly as you have shown, starting with "State" in cell A1.
This code is going to write data so please test it on a back-up copy of your work!

Code:
Sub WhoseWeight()

    Dim lRow As Long, a As Long, s As Long, e As Long
    Dim state(1 To 13) As String
    Dim ethnic(1 To 5) As String
    Dim age(1 To 3) As String
    Dim wn As Long

    age(1) = "18-29"
    age(2) = "30-59"
    age(3) = "60+"
'====================You have to correct your states==============
    state(1) = "AZ"
    state(2) = "NM"
    state(3) = "NY"
    state(4) = "NJ"
    state(5) = "CA"
    state(6) = "FL"
    state(7) = "TX"
    state(8) = "OK"
    state(9) = "NV"
    state(10) = "WV"
    state(11) = "GA"
    state(12) = "OH"
    state(10) = "AK"
    state(11) = "NC"
    state(12) = "VT"
    state(13) = "MA"
'=====================You have to correct your ethnicities===================
    ethnic(1) = "black"
    ethnic(2) = "caucasian"
    ethnic(3) = "asian"
    ethnic(4) = "mexican"
    ethnic(5) = "persian"
    

    lRow = Range("A" & Rows.Count).End(xlUp).Row
    For a = 1 To 3
        For s = 1 To 13
            For e = 1 To 5
'======================You will have to finish adding your if designations below for the weight categories- All 165 of them
'                                        Just put in the number part of the weight as "wn = ??"
'                                        The word "Weight" will appear from the code below.
'                                        Following along from above:  a3/s10/e4 would be a 60+ / West Virginia / Mexican

            If a = 1 And s = 1 And e = 1 Then wn = 1   ' this is a1/s1/e1 = Weight1
            If a = 1 And s = 1 And e = 2 Then wn = 2   ' this is a1/s1/e2 = Weight2
            If a = 1 And s = 1 And e = 3 Then wn = 3
            If a = 1 And s = 1 And e = 4 Then wn = 4
            
            With Worksheets("Sheet1")
                .AutoFilterMode = False
       
                With .Range("A1:D" & lRow)
                    .AutoFilter field:=1, Criteria1:=state(s)
                    .AutoFilter field:=2, Criteria1:=ethnic(e)
                    .AutoFilter field:=3, Criteria1:=age(a)
                    .SpecialCells (xlCellTypeVisible)
                    .SpecialCells(xlCellTypeBlanks).Value = "Weight" & wn
                End With
            .AutoFilterMode = False
            End With
            Next
        Next
    Next
    
End Sub


HTH

igold
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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