尝试获取单元格AI和BM的值时出现错误1004

时间:2018-09-06 06:23:22

标签: excel vba excel-vba

我正在尝试查找某些强制性值是否填充在特定的单元格中。但是,当我尝试查找单元格AI和BM的值时,excel会抛出错误1004,但它对单元格X和Y仍然有效。您知道如何更正此错误吗?

第1行有我的标题。

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

1 个答案:

答案 0 :(得分:2)

问题在于,在MsgBoxAI的{​​{1}}行中,BM返回一个值数组,因为& Range("AI").value &整列

您可能打算使用AI仅返回标头名称。

& Range("AI1").value &相同。