第一次循环时循环和循环停止时出现文件错误

时间:2017-06-14 07:13:36

标签: excel excel-vba vba

这个想法是将信息从命名工作簿提取到主工作簿(以只读方式)。我已经制定了一个代码来提取信息并使用循环函数。不知何故,循环函数不起作用,因为它只给出了第一个循环的结果。

此外,经过几次,它开始给我一个

  

申请/定义的错误

在这一行

billNumberNamesheet =sakinahWorkBook.Worksheets("Sakinah").Range(cell).Value

请看一下然后回复我。谢谢! :)

Dim sakinahWorkBookPath As String
    Dim sasitaWorkBookPath As String

    excelFilePath = Application.ActiveWorkbook.Path
    sakinahWorkBookPath = excelFilePath & "\Sakinah.xlsm"
    sasitaWorkBookPath = excelFilePath & "\Sasita.xlsm"

    Set sakinahWorkBook = Workbooks.Open(sakinahWorkBookPath)
    Set sasitaWorkBook = Workbooks.Open(sasitaWorkBookPath)

    For x = 4 To readLastCellNameSheet
        cell = "A" & x
        billNumber = ThisWorkbook.Worksheets("Master").Range(cell).Value
        If Len(billNumber) = 0 Then Exit For

        For M = 4 To readLastCellNameSheet
            SheetName = "SAKINAH"
            cell = "A" & N

            'giving application defined/object error.
            billNumberNamesheet =sakinahWorkBook.Worksheets("Sakinah").Range(cell).Value

            If Len(billNumberNamesheet) = 0 Then Exit For

            If billNumberNamesheet = billNumber Then
                cell = "R" & N & ":" & "AV" & N
                copycell = "R" & x & ":" & "AV" & x
                sakinahWorkBook.Worksheets("Sakinah").Range(cell).Copy ThisWorkbook.Worksheets("Master").Range(copycell) 
            End If
        Next M

        For N = 4 To readLastCellNameSheet
            SheetName = "SASITA"
            cell = "A" & N
            billNumberNamesheet = sasitaWorkBook.Worksheets("Sasita").Range(cell).Value
            If Len(billNumberNamesheet) = 0 Then Exit For

            If billNumberNamesheet = billNumber Then
                cell = "R" & N & ":" & "AV" & N
                copycell = "R" & x & ":" & "AV" & x
                sasitaWorkBook.Worksheets("Sasita").Range(cell).Copy ThisWorkbook.Worksheets("Master").Range(copycell)  
            End If
        Next N
    Next x

    ' CLOSE THE SOURCE FILE.
    sakinahWorkBook.Close True             ' FALSE - DON'T SAVE THE SOURCE FILE.
    Set sakinahWorkBook = Nothing

    ' CLOSE THE SOURCE FILE.
    sasitaWorkBook.Close True             ' FALSE - DON'T SAVE THE SOURCE FILE.
    Set sasitaWorkBook = Nothing
End Sub

0 个答案:

没有答案