Home » excel » excel – Error 1004 when trying to get values for cells AI and BM

excel – Error 1004 when trying to get values for cells AI and BM

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to find if some mandatory values are filled in particular cells or not. But When I try to find value of cell AI and BM, excel throws me Error 1004 but it works fine for cells X and Y. Any idea how to correct this?

Row 1 has my headers.

Private Sub CommandButton1_Click()

    Dim value As Range, sheetRange As Range
    Dim j As Integer
    Dim lRow As Integer

    Set sheetRange = Sheet4.Range("A2:BM65536")

    On Error Resume Next
    Set value = Intersect(sheetRange.EntireRow.SpecialCells(xlConstants), sheetRange)

    On Error GoTo 0

    If value Is Nothing Then
        MsgBox "Good to go"
        Exit Sub

    Else
        lRow = Cells(Rows.Count, 1).End(xlUp).Row

        For j = 2 To lRow

            If Len(Sheet4.Range("X" & j).value) = 0 Then
                MsgBox "Enter value for: Data sheet" & vbNewLine & "Column name: " & Range("X1").value & vbNewLine & "Cell X" & j, vbExclamation
                Exit Sub
            End If

            If Len(Sheet4.Range("Y" & j).value) = 0 Then
                MsgBox "Enter value for: Data sheet" & vbNewLine & "Column name: " & Range("Y1").value & vbNewLine & "Cell Y" & j, vbExclamation
                Exit Sub
            End If

            If Len(Sheet4.Range("AB" & j).value) = 0 Then
                MsgBox "Enter value for: Data sheet" & vbNewLine & "Column name: " & Range("AB1").value & vbNewLine & "Cell AB" & j, vbExclamation
                Exit Sub
            End If

            If Len(Sheet4.Range("AI" & j).value) = 0 Then
                MsgBox "Enter value for: Data sheet" & vbNewLine & "Column name: " & Range("AI").value & vbNewLine & "Cell AI" & j, vbExclamation
                Exit Sub
            End If

            If Len(Sheet4.Range("BM" & j).value) = 0 Then
                MsgBox "Enter value for: Data sheet" & vbNewLine & "Column name: " & Range("BM").value & vbNewLine & "Cell BM" & j, vbExclamation
                Exit Sub
            End If

        Next

  End If

    End Sub
How to&Answers:

The issue is that in your MsgBox line of AI and BM the & Range("AI").value & returns an array of values, because AI is the whole column.

You probably meant to use & Range("AI1").value & to return the header name only.

Same for & Range("BM").value &.