运行时错误1004.选择无效,有几个可能的原因

时间:2016-05-17 05:41:10

标签: excel-vba vba excel

我正在尝试使用excel VBA中的依赖组合框,但我收到以下错误:我无法理解为什么会得到。帮助会很棒。

Run-time error '1004': The selection is not valid, There are several possible reasons:

代码:

.alignright {
    width: 103px;
    height: 192px;
}

1 个答案:

答案 0 :(得分:1)

您的最终With不正确。尝试:

With Worksheets("Sheet5")
    For colsCounter = 1 To lastColumn
        With .Columns(colsCounter)
            lastRow = Sheet5.Cells(Sheet5.Rows.Count, colsCounter).End(xlUp).Row
            With .Range(.Cells(1, colsCounter), .Cells(lastRow, colsCounter))
                .CreateNames Top:=True
            End With
        End With
    Next
End With

如果它是前一个块的子项,则只能嵌套With块:

'// This will work fine
With Sheets("Sheet1")
    With .Range("A1")    '// Notice the '.' before Range()
        .Value = "test"
    End With
End With

'// This will not
With Sheets("Sheet1")
    .Range("A1").Value = "test"
    With Sheets("Sheet2")
        .Range("A1").Value = "test"
    End With
End With

在第二个示例中,您必须在开始下一个With块之前关闭它。