设置范围相等时VBA错误1004

时间:2016-06-09 18:13:32

标签: excel vba excel-vba

无论我做什么,都不能让这个错误消失。当前版本的代码是完全拼写每个引用的努力。我在“BOM组件的设定范围”行之后收到错误。大多数主题指出使用.Range(Cells())而不指定单元格所在的位置将导致此错误,但我已经完成了完整的过度杀伤参考,所以我不确定为什么它仍然出错。这里只有一本工作簿。

@Scott Craner @BruceWayne我终于明白了; Oracle报告以一种非常漂亮的格式出现,而循环中的第一张表格导致范围实际上是不相等的。一旦我删除了那张表就完美了。

        Sub Macro3()
            '
            ' Macro3 Macro
            '
            ' Keyboard Shortcut: Ctrl+c
            '
            Dim ws As Worksheet
            Dim lastRow As Integer
            Dim summaryRow As Integer
            Dim currentSheetRows As Integer
            Dim i As Integer
            Dim j As Integer
            summaryRow = 2

                For Each ws In ActiveWorkbook.Worksheets
                    If ws.Range("L3") = "" Then
                        currentSheetRows = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row
                        i = summaryRow
                        j = summaryRow

                        'set range for item # and item UOM'
                        For j = summaryRow To (summaryRow + (currentSheetRows - 3))
                        Sheets("Summary").Cells(j, 1).Value = ws.Cells(2, 1).Value
                        Next j

                        For i = summaryRow To (summaryRow + (currentSheetRows - 3))
                        Sheets("Summary").Cells(i, 2).Value = ws.Cells(2, 1).Value
                        Next i

                        'set range for BOM components'
                        ThisWorkbook.Worksheets("Summary").Range(ThisWorkbook.Worksheets("Summary").Cells(summaryRow, 3), ThisWorkbook.Worksheets("Summary").Cells((summaryRow + (currentSheetRows - 3)), 6)).Value = ThisWorkbook.Worksheets(ws.Name).Range(ThisWorkbook.Worksheets(ws.Name).Cells(3, 6), ThisWorkbook.Worksheets(ws.Name).Cells(currentSheetRows, 9)).Value
                        summaryRow = summaryRow + currentSheetRows
                    End If
                Next ws
            End Sub

3 个答案:

答案 0 :(得分:1)

哇 - 首先,检查范围是否相同。然后,我强烈建议您为工作表名称和范围使用一些变量:

Sub t()
Dim summaryWS As Worksheet
Dim otherWS As Worksheet

Set summaryWS = ThisWorkbook.Sheets("Summary")
Set otherWS = ThisWorkbook.Worksheets(ws.Name)

Dim copyRng As Range, pasteRng As Range

With summaryWS
    Set copyRng = .Range(.Cells(SummaryRow, 3), .Cells((SummaryRow + (currentSheetRows - 3)), 6))
End With

With otherWS
    Set pasteRng = .Range(.Cells(3, 6), .Cells(currentSheetRows, 9))
End With

pasteRng.Value = copyRng.Value

End Sub

这对你的问题有帮助吗?

答案 1 :(得分:1)

你正在使用

ActiveWorkbook

设置循环当前表

然后

ThisWorkbook

在复制/粘贴值语句中出现错误

然后,您运行宏的工作簿(ThisWorkbook)可能与当前活动的工作簿(ActiveWorkbook)不同,而工作表是您正在循环的工作表

在这种情况下,只需将ThisWorkbook更改为ActiveWorkbook

即可

答案 2 :(得分:0)

问题在于:

ThisWorkbook.Worksheets("Summary").Range(ThisWorkbook.Worksheets("Summary").Cells(summaryRow, 3), ThisWorkbook.Worksheets("Summary").Cells((summaryRow + (currentSheetRows - 3)), 6)).Value = ThisWorkbook.Worksheets(ws.Name).Range(ThisWorkbook.Worksheets(ws.Name).Cells(3, 6), ThisWorkbook.Worksheets(ws.Name).Cells(currentSheetRows, 9)).Value

下标超出范围。

尝试

With Sheets(ws.name)
    .Cells({var},{var}).Value = {var}