This is my first Array in VBA for Excel, but I need some help to optimize the code and try to reduce the number of If
statements.
The long and short of the code is that it checks to see if there is a Customer name in Column B and checks that against Column A; if there is no value in Column A then an input box opens to prompt the user to add the CIF number to the specific named range.
Cells B2 through B9 will always have a value, but not every cell B2 through B9 will be used. Cells A2 through A9 will not always have a value.
Below is the Code:
Sub CheckCIF()
Dim fileArray
Dim finalRow As Long
Dim targetCol As Long
With Sheets("Loan Data")
finalRow = .Range("B" & Rows.Count).End(xlUp).Row
targetCol = .Range("A1").EntireRow.Find("CIF #").Column
fileArray = Array(.Range("B2:B" & finalRow), _
.Range(.Cells(2, targetCol), .Cells(finalRow, targetCol)))
End With
'fileArray(1)(1) Number in first parenthesis is the column and the number in the second parenthesis is the row
If fileArray(1)(1) = vbNullString And fileArray(0)(1) <> vbNullString Then
With Sheets("Loan Data")
.Range("CIF_1") = InputBox("Please enter the CIF Number for " & vbCrLf & fileArray(0)(1))
End With
End If
If fileArray(1)(2) = vbNullString And fileArray(0)(2) <> vbNullString Then
With Sheets("Loan Data")
.Range("CIF_2") = InputBox("Please enter the CIF Number for " & vbCrLf & fileArray(0)(2))
End With
End If
If fileArray(1)(3) = vbNullString And fileArray(0)(3) <> vbNullString Then
With Sheets("Loan Data")
.Range("CIF_3") = InputBox("Please enter the CIF Number for " & vbCrLf & fileArray(0)(3))
End With
End If
If fileArray(1)(4) = vbNullString And fileArray(0)(4) <> vbNullString Then
With Sheets("Loan Data")
.Range("CIF_4") = InputBox("Please enter the CIF Number for " & vbCrLf & fileArray(0)(4))
End With
End If
If fileArray(1)(5) = vbNullString And fileArray(0)(5) <> vbNullString Then
With Sheets("Loan Data")
.Range("CIF_5") = InputBox("Please enter the CIF Number for " & vbCrLf & fileArray(0)(5))
End With
End If
If fileArray(1)(6) = vbNullString And fileArray(0)(6) <> vbNullString Then
With Sheets("Loan Data")
.Range("CIF_6") = InputBox("Please enter the CIF Number for " & vbCrLf & fileArray(0)(6))
End With
End If
If fileArray(1)(7) = vbNullString And fileArray(0)(7) <> vbNullString Then
With Sheets("Loan Data")
.Range("CIF_7") = InputBox("Please enter the CIF Number for " & vbCrLf & fileArray(0)(7))
End With
End If
If fileArray(1)(8) = vbNullString And fileArray(0)(8) <> vbNullString Then
With Sheets("Loan Data")
.Range("CIF_8") = InputBox("Please enter the CIF Number for " & vbCrLf & fileArray(0)(8))
End With
End If
End Sub
targetCol = .Range("A1").EntireRow.Find("CIF #").Column
and then in thefileArray(1)(1)
where the 1 in the first () is column A. \$\endgroup\$