0
votes

In my workbook I have 2 worksheets: Sheet1 and Sheet2.

In Sheet1 I have the following data set:

enter image description here

In Sheet2 I have the following data set:

enter image description here

I need to create a code that will do the following:

  • Populate the score columns ("Score of SpeGro", "Score of PrimSpe", etc.)

For example, for the "Score of SpeGro" column it needs to:

  • Search in Sheet1 the column header corresponding to SpeGro (in this case it's column 4);
  • The values of column 4 need to match the values in column 3 of Sheet2.
  • Only consider the values in Sheet2 with the DIMENSION "SpeGro" (in this case);
  • Only consider the values with PrdInd (Sheet1) = PrdInd (Sheet2).

Extra info: I have a INDEX-MATCH formula that works if I only had DIMENSION:

For k = 2 To RowNum
    tWb.Sheets("Sheet1").Cells(k, 6).Value = Application.IfError(Application.Index(tWb.Sheets("Sheet2").Range("D:D"), Application.Match(tWb.Sheets("Sheet1").Cells(k, 4), tWb.Sheets("Sheet2").Range("C:C"), 0)), 0)
Next k

Any idea on how I can achieve this? enter image description here

2

2 Answers

0
votes

This is just to give you an idea on how you can tackle this task. But the code is working if you want to try it.

'task: Populate the score columns ("Score of SpeGro", "Score of PrimSpe", etc.)
'if conditions are met

Sub Whatever()

Dim strSearch As String
Dim aCell As Range
Dim col_n As Integer
Dim last_row As Long
Dim first_row As Byte
Dim Count As Long

'Search in Sheet1 the column header corresponding to
'"Score of SpeGro" (in this case it's column 6)

'CONFIG
'-------------
strSearch = "Score of SpeGro"
first_row = 2 'first row of the data sets in sheet 1 and 2
'-------------    

Set aCell = Sheet1.Rows(1).Find(What:=strSearch, LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
    
col_n = aCell.Column

'column numbers sheet1

'Scoreof PrimSpe column numner = col_n +1
'SpeGro column number = col_n - 2
'Prdlnd column number = col_n - 5

last_row = Sheets("Sheet1").Cells(Rows.Count, col_n - 5).End(xlUp).Row

For Count = first_row To last_row

If Sheets("Sheet1").Cells(Count, col_n - 2) = Sheets("Sheet2").Cells(Count, 3) _
And Sheets("Sheet2").Cells(Count, 2) = "SpeGro" _
And Sheets("Sheet1").Cells(Count, col_n - 5) = Sheets("Sheet2").Cells(Count, 1) Then

Sheets("Sheet1").Cells(Count, col_n) = "Put something here"

End If

Next Count

End Sub
0
votes
Option Explicit

Sub Button1_Click()

'task: Populate the score columns ("Score of SpeGro", "Score of PrimSpe", etc.)
'if conditions are met

Dim strSearch1 As String, strSearch2 As String
Dim aCell1 As Range, aCell2 As Range
Dim col_n1 As Integer, col_n2 As Integer
Dim last_row1 As Long, last_row2 As Long
Dim first_row As Byte
Dim Count As Long
Dim myArray As Variant, element As Variant

'Search in Sheet1 the column header corresponding to
'"Score of SpeGro" (in this case it's column 6)

myArray = Array("Specialty Grouping", "Primary Specialty")

'strSearch1 = "Score of Specialty Grouping"
'strSearch2 = "Specialty Grouping"

For Each element In myArray
    Set aCell1 = Sheet1.Rows(1).Find(What:="Score of " & element)
    Set aCell2 = Sheet1.Rows(1).Find(What:=element)
        
    col_n1 = aCell1.Column
    col_n2 = aCell2.Column
    
    'column numbers sheet1
    
    'SpeGro column number = col_n2
    
    last_row1 = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    last_row2 = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
    
    'The values of col_n2 need to match the values in column 3 of USER_INPUTS.
    
    For Count = 2 To last_row1
        Sheets("Sheet1").Cells(Count, col_n1) = Application.Index(ThisWorkbook.Sheets("Sheet2").Range("D2:D" & last_row2), _
        Application.Match(ThisWorkbook.Sheets("Sheet1").Cells(Count, 1), ThisWorkbook.Sheets("Sheet2").Range("A2:A" & last_row2), 0) * _
        Application.Match(ThisWorkbook.Sheets("Sheet1").Cells(1, col_n2), ThisWorkbook.Sheets("Sheet2").Range("B2:B" & last_row2), 0) * _
        WorksheetFunction.IfError(Application.Match(ThisWorkbook.Sheets("Sheet1").Cells(Count, col_n2), ThisWorkbook.Sheets("Sheet2").Range("C2:C" & last_row2), 0), 0))
    Next Count
Next element
    
End Sub