Excel Visual Basic应用程序定义或对象定义的错误

时间:2014-06-02 16:51:34

标签: vba excel-vba runtime-error worksheet-function excel

所以我在一个工作簿中有两个excel工作表。有一个更大的名为" data"较小的一个名为" Sheet1。"下面的代码首先创建一个名为" DailyReport"并比较两张纸。如果来自" Sheet1"的B列中的一个单元格与"数据"的B列中的一个单元格相同,它应该将该行复制到"数据"到新表" DailyReport。"下面是代码。这是一个宏。现在它一直说"无效的过程调用或参数"错误。

Sub DailyReportGenerator()
Sheets.Add.Name = "DailyReport"

Application.ScreenUpdating = False

Dim StartNumber As Integer
Dim EndNumber As Integer
Dim StartNumber2 As Integer

EndNumber = 9999

For StartNumber = 1 To EndNumber
    For StartNumber2 = 1 To EndNumber

    If Worksheets("Sheet1").Cells(StartNumber, "B").Value = Worksheets("data").Cells(StartNumber2, "B").Value Then
        Sheets("data").Range(Sheets("data").Cells(StartNumber, "B"), Sheets("data").Cells(StartNumber, "CA")).Copy Sheets("DailyReport").Cells("StartNumber, B")
    End If

    Next StartNumber2
Next StartNumber

Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

<强>编辑:

Sub DailyReportGenerator()

Const EndNumber As Long = 999

Dim shtRpt As Worksheet, shtData As Worksheet, shtSheet1 As Worksheet
Dim rwNumSht1 As Long, rwNumData As Long

Set shtRpt = Sheets.Add()
shtRpt.Name = "DailyReport"

Set shtData = Worksheets("data")
Set shtSheet1 = Worksheets("Sheet1")

Application.ScreenUpdating = False

For rwNumSht1 = 1 To EndNumber
    For rwNumData = 1 To EndNumber

    If shtSheet1.Cells(rwNumSht1, "B").Value = shtData.Cells(rwNumData, "B").Value Then
        With shtData
        .Range(.Cells(rwNumData, "B"), .Cells(rwNumData, "CA")).Copy _
                                           shtRpt.Cells(rwNumSht1, "B")
        End With
    End If

    Next rwNumData
Next rwNumSht1

Application.ScreenUpdating = True
End Sub