Range.Clear上的VBA运行时错误1004

时间:2016-06-03 18:46:53

标签: excel vba runtime-error

关于这个错误有很多线索,但无论我尝试什么,我都无法解决这个问题。大多数人都说当你尝试在非活动工作表上调用方法时会发生这种情况,但你不应该这样做。错误在第28行。谢谢。

            Private Sub CommandButton1_Click()

            Dim x As Integer 
            Dim boisePaste As Integer
            Dim jrgPaste As Integer
            Dim master As Integer
            Dim lastRow As Integer
            Dim bookCount As Integer
                bookCount = Application.Workbooks.Count


            For x = 1 To bookCount
             If Left(Application.Workbooks(x).Name, 14) = "ITEM_INVENTORY" Then
                boisePaste = x
             ElseIf Left(Application.Workbooks(x).Name, 6) = "report" Then
                jrgPaste = x
             ElseIf Left(Application.Workbooks(x).Name, 8) = "Portland" Then
                master = x
             End If
            next x


            'Unhide sheets and delete Boise range'

            Application.ActiveWorkbook.Sheets("BoisePaste").Visible = True
            Sheets("JRGpaste").Visible = True
            lastRow = Sheets("BoisePaste").Cells(Rows.Count, "B").End(xlUp).Row
            Sheets("BoisePaste").Range(Cells(1,2), Cells(lastRow, 23)).Clear


            'Open Boise file and copy range, paste in master'

            Application.Workbooks(boisePaste).Activate
                With ActiveSheet
                    .Range(.Cells(1,1), .Cells((.Cells(Rows.Count, "A").End(xlUp).Row),22)).Copy
                End With

            Application.Workbooks(master).Sheets("BoisePaste").Range(B1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
            Application.CutCopyMode = False


            'Open JRG report and copy range, paste in master'

            Application.Workbooks(jrgPaste).Activate
            ActiveSheet.Cells.Copy
            Application.Workbooks(master).Sheets("JRGpaste").Range(A1).Paste
            Application.CutCopyMode = False


            'Refresh pivot tables; hide sheets'
            Application.Workbooks(master).Activate

            With ActiveWorkbook
                .RefreshAll
                .RefreshAll
                .Sheets("BoisePaste").Visible = False
                .Sheets("BoisePaste").Visible = False
            End With

            End Sub

1 个答案:

答案 0 :(得分:4)

您需要明确说明您希望res.send(doneTemplate.render({username: req.body.user})); 使用哪个工作表,并且其他此类范围使用(Rows.CountColumnsRows等)将会启用。

试试这个:

Cells

所以,仔细阅读你的代码并确保你到处都这样做...即。在Sheets("BoisePaste").Range(Sheets("BoisePaste").Cells(1,2), Sheets("BoisePaste").Cells(lastRow, 23)).Clear 中,您没有对.Range(.Cells(1,1), .Cells((.Cells(Rows.Count, "A").End(xlUp).Row),22)).Copy执行此操作,因此也请在此处添加工作表,以防止任何意外操作。

或许可以这样想,就行了 Rows.Count

VBA正在读取

  

myVariable = Sheets("mySheet").Range(Cells(1,1),Cells(1,2)).Value中,查找范围。什么范围?嗯,用户说mySheetCells(1,1),但是他想要哪张?当前的活动表名为Cells(1,2) ...他指定yourSheet应该在哪里(表单Range),但他没有在mySheet上,所以我不知道知道他想要什么! Cells()mySheet cells(1,1) ??

(是的,这正是计算机的想法:P)

编辑:我经历过并尝试帮助收紧您的代码。但是,正如你可能看到的那样,我对你想做什么并不是很积极,但这应该给你一些帮助/见解:

yourSheet cells(1,1)