Below is a sample VBA code for your requirement. This code will go through the Person Responsible sheet, find the corresponding sheets mentioned in column C, retrieve the specified values from those sheets, and then copy the value to column D.
To implement this, you need to open your Excel workbook, press ALT + F11 to open the Visual Basic for Applications (VBA) editor, insert a new module (by right-clicking on any of the objects for your workbook in the Project Explorer, then choosing Insert > Module), and copy the following code into the new module:
Sub CopyValuesToPersonResponsible()
Dim wsPersonResponsible As Worksheet
Dim wsTarget As Worksheet
Dim lastRow As Long
Dim i As Long
Dim j As Long
Dim rngBA As Range
Dim rngBB As Range
Dim valueToMatch As Variant
Dim found As Range
Dim targetSheetName As String
' Set the "Person Responsible" worksheet
Set wsPersonResponsible = ThisWorkbook.Sheets("Person Responsible")
' Determine the last row in column C of "Person Responsible"
lastRow = wsPersonResponsible.Cells(wsPersonResponsible.Rows.Count, 3).End(xlUp).Row
' Loop through each entry in column C
For i = 2 To lastRow
' Get the sheet name from column C
targetSheetName = wsPersonResponsible.Cells(i, 3).Value
On Error Resume Next
' Attempt to set the target worksheet
Set wsTarget = ThisWorkbook.Sheets(targetSheetName)
On Error GoTo 0 ' Turn back on normal error handling
' Check if the target worksheet was found
If Not wsTarget Is Nothing Then
' Define the ranges BA2:BA30 and BB2:BB30
Set rngBA = wsTarget.Range("BA2:BA30")
Set rngBB = wsTarget.Range("BB2:BB30")
' Get the value to match from column B in "Person Responsible"
valueToMatch = wsPersonResponsible.Cells(i, 2).Value
' Loop through the values in the BA range
For j = 1 To rngBA.Rows.Count
' Check if the value in column BA matches the value to match
If rngBA.Cells(j, 1).Value = valueToMatch Then
' If a match is found, copy the corresponding BB value to column D
wsPersonResponsible.Cells(i, 4).Value = rngBB.Cells(j, 1).Value
Exit For ' Exit the loop once the first match is found
End If
Next j
Else
' Optional: Handle the case where the sheet is not found
wsPersonResponsible.Cells(i, 4).Value = "Sheet Not Found"
End If
' Reset the target sheet for the next iteration
Set wsTarget = Nothing
Next i
MsgBox "Values copied to 'Person Responsible'."
End Sub
How the Code Works:
- Initialization: The code initializes the
wsPersonResponsible as the "Person Responsible" worksheet.
- Row Iteration: It loops through the rows starting from row 2 in column C to the last filled row.
- Sheet Name and Existence Check: It retrieves the sheet name from column C and checks if such a sheet exists in the workbook.
- Value Matching: It then loops through the range BA2:BA30 in the corresponding sheet, checking for a match with the value from column B of the "Person Responsible" sheet.
- Value Copying: If a match is found, it copies the value from column BB of the corresponding sheet to column D of the "Person Responsible" sheet.
- Error Handling: If the sheet does not exist, it places a notification ("Sheet Not Found") in column D.
Running the Code:
To run this code:
- Press F5 in the VBA editor or close the editor and run the macro from the Excel interface (via Developer > Macros).
- Make sure that your
Person Responsible sheet is properly formatted and filled with the required data, along with the specified sheets.
Note:
Before running any VBA code, make sure to have a backup of your data to prevent any accidental loss.