尝试选择预定义范围时出现错误1004

时间:2018-03-27 19:22:07

标签: excel

在查看引用问题后(是否在.Range中必需?)我尝试删除点,添加工作表(“sheet1”),删除“Cells”对象前面的点。如果我运行“ActiveSheet”下面的代码,我清除“错误1004”,但我永远不会去“Sheet1”并选择我想要定义的“k - > l”范围。请指教。

ActiveSheet.Range(Cells(k,1),Cells(1,17))。选择

K L 保存我尝试选择输入数据透视表的范围的行号。一切正常,直到我到达:

ThisWorkbook.Worksheets ("Sheet1").Range (Cells (k, 1), Cells (l, 17).Select

我收到错误1004。

Public Sub Create_Report()

        'Define range of report
        ww_from = Application.InputBox _
            (prompt:="From what is the workweek do you want to report?", Type:=1)

        ww_to = Application.InputBox _
        (prompt:="To what workweek do you want to report?", Type:=1)


        'Find first row of data set locations
        With ThisWorkbook.Worksheets("Sheet1").Range("C:C")

            Set First = .Find(ww_from)

            'Locate data set for report by row number
            If Not First Is Nothing Then k = First.Row

            'Find last row of data set locations
            Set Last = .Find(ww_to + 1)

            'Locate data set for report by row number
            If Not Last Is Nothing Then
                l = Last.Row - 1
            Else
                l = k
            End If

            ThisWorkbook.Worksheets("Sheet1").Range(Cells(k, 1), Cells(l, 17)).Select

        End With

1 个答案:

答案 0 :(得分:1)

假设k和l是数字且大于零(这意味着错误处理包括你的Finds没有检索任何内容)请尝试以下方法:

ERROR CONTEXT {…}
elDef: Object { nodeIndex: 21, bindingIndex: 9, outputIndex: 1, … }​
elView: Object { def: {…}, parent: {…}, state: 1052, … }
nodeDef: Object { nodeIndex: 23, bindingIndex: 12, outputIndex: 2, … }​
nodeIndex: 23​view: Object { def: {…}, parent: {…}, state: 1052, … }​__proto__: {…}​​component:
Getter​​componentRenderElement: 
Getter​​constructor: function DebugContext_()​​context: Getter​​elOrCompView: 
Getter​​injector: Getter​​logError: function logError()​​providerTokens: 
Getter​​references: Getter​​renderNode: 
Getter​​__proto__: Object { … } NavigationComponent.html:34:71

并避免。选择是否需要(可能不是)

所以也许是一个简单的检查:

With ThisWorkbook.Worksheets("Sheet1")
    .Range(.Cells(k, 1), .Cells(l, 17)).Select
End With

编辑:

供您使用

If k > 0 And l > 0 Then
    With ThisWorkbook.Worksheets("Sheet1")
        .Range(.Cells(k, 1), .Cells(l, 17)).Select ' <====get rid of this if possible
    End With
End If