简体   繁体   中英

Excel Help: Using text from one cell to reference values in another worksheet

I have a worksheet that lists all the Representatives in my state in one column and the counties that they represent in another column. Some of them represent one county, while others have multiple counties (some cells say "A and B" and others have "A, B, & C").

On a different worksheet, I have a list of all counties in one column, a 2nd column of numbers of people diagnosed with a certain disease, and a 3rd column of number of total people in that county living with the disease.

Basically, I want to be able to merge these together so that I will have something like this:

Column 1: John Smith

Column 2: CountyX, CountyY, & County Z

Column 3: 389 (sum all 3 county values found on 2nd worksheet for # of diagnosed)

Column 4: 4392 (sum all 3 county values found on 2nd worksheet for total)

Is there any way to utilize the 2nd column on the first worksheet (counties each representative represents) to automatically refer to the 2nd worksheets values and add them together to give each representative a total number of cases under them?

I could do it manually, of course. However, I would like to it to be able to be easily changed for next year.

My gut instinct is to use VBA to take column 2, explode the list, search worksheet 2 for the values take from column 2, then build a formula in column 3 to reference the values found in sheet 2. Same thing for column 4. Something like:

Sub Stuff()
    Dim numberOfReps As Integer
    Dim numberOfCounties As Integer
    numberOfReps = 20
    numberOfCounties = 25

    Dim i As Integer
    Dim counties As String
    Dim diagnosedEquation As String
    Dim livingEquation As String
    Dim rawVal As String

    For i = 1 To numberOfReps
        rawCounties = Sheets("Sheet1").cells(i, 2).Value
        countiesArray = Split(rawCounties, ", ")

        For j = 1 To numberOfCounties
            rawVal = Sheets("Sheet2").cells(j, 1).Value
            If (ListContains(rawVal, countiesArray)) Then
                If (livingEquation = "") Then
                    livingEquation = "=SUM(Sheet2!C" & j
                Else
                    livingEquation = livingEquation & "+Sheet2!C" & j
                End If
                If (diagnosedEquation = "") Then
                    diagnosedEquation = "=SUM(Sheet2!B" & j
                Else
                    diagnosedEquation = diagnosedEquation & "+Sheet2!B" & j
                End If
            End If
        Next j

        If (Not diagnosedEquation = "") Then diagnosedEquation = diagnosedEquation & ")"
        If (Not livingEquation = "") Then livingEquation = livingEquation & ")"

        Sheets("Sheet1").cells(i, 3).Value = diagnosedEquation
        Sheets("Sheet1").cells(i, 4).Value = livingEquation
        diagnosedEquation = ""
        livingEquation = ""
    Next i
End Sub

Private Function ListContains(needle As String, haystack As Variant)
    For Each straw In haystack
        If (needle = straw) Then ListContains = True
    Next straw
End Function

I have tested this and it will sum stuff up. You'll probably have to tweak it a bit for your setup, such as the sheet names (or indexes).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM