The below snippet assigns the value in B1 of sheet1 to the personName variable. It then loops through each sheet in the workbook and, if the value in column B is equal to personName, inserts corresponding values from the sheet "SenDates" into the adjacent cells in columns C and D. The case is based on the value in cell A12 of each sheet. Case 116 inserts the corresponding values from the third and fourth columns of the SenDates sheet, Case 117 inserts values from fifth and sixth columns, Case 120 from the seventh and 8th etc. Each Case picks data from the next 2 columns in the SenDates sheet.
It works as intended, however, I am ending up with over 100 cases, making for some disgusting code.
What would be a better way to do this?
It works as intended, however, I am ending up with over 100 cases, making for some disgusting code.
What would be a better way to do this?
Code:
Sub senDate1()
Dim i As Integer, j As Integer
Dim ws As Worksheet
Dim personName As String
personName = Sheets("Sheet1").Range("B1").Value
For Each ws In Worksheets
If ws.Name <> "Sheet1" And ws.Name <> "SenDates" Then
Select Case ws.Range("A12").Value
Case "116"
For i = 150 To 2 Step -1
If ws.Cells(i, 2).Value = personName Then
For j = 625 To 2 Step -1
If Sheets("SenDates").Cells(j, 2).Value = personName Then
ws.Cells(i, 4).Value = Sheets("SenDates").Cells(j, 3).Value
ws.Cells(i, 5).Value = Sheets("SenDates").Cells(j, 4).Value
End If
Next j
End If
Next i
Case "117"
For i = 150 To 2 Step -1
If ws.Cells(i, 2).Value = personName Then
For j = 625 To 2 Step -1
If Sheets("SenDates").Cells(j, 2).Value = personName Then
ws.Cells(i, 4).Value = Sheets("SenDates").Cells(j, 5).Value
ws.Cells(i, 5).Value = Sheets("SenDates").Cells(j, 6).Value
End If
Next j
End If
Next i
Case "120"
For i = 150 To 2 Step -1
If ws.Cells(i, 2).Value = personName Then
For j = 625 To 2 Step -1
If Sheets("SenDates").Cells(j, 2).Value = personName Then
ws.Cells(i, 4).Value = Sheets("SenDates").Cells(j, 7).Value
ws.Cells(i, 5).Value = Sheets("SenDates").Cells(j, 8).Value
End If
Next j
End If
Next i
...