修复以防止多个宏运行覆盖另一个数据

时间:2017-06-13 11:41:50

标签: excel vba excel-vba

*修改

好吧,改变方向。我没有循环遍历文件,而是为每个文件创建了10个单独的宏,并在单个按钮下运行。每个宏都可以完美地运行。但是,当我使用下面的代码将它们全部一起运行时,每个宏都会一个接一个地运行,但每次运行一个新的宏时,它都会覆盖之前粘贴的数据。有关如何保留以前宏的更改的任何建议?我尝试过最简单的想法,那就是在每个时间间隔保存工作簿但是没有用。救命?

Sub UpdateDate_Click()
    Call Sakinah_Update
    Call Sasita_Update
    Call Mashitah_Update
    Call NoorAzawati_Update
    Call NurHidayah_Update
    Call NurulIzzati_Update
    Call NurulShazwani_Update
    Call Rohaizad_Update
    Call Shahrani_Update
    Call Shahrudin_Update

    MsgBox "Updated Succesully"
End Sub

*编辑编辑
我可以添加这个是我为所有宏复制的代码。更改了所有的工作表名称。是因为这个代码,宏会相互覆盖吗?

Sub Sasita_Update()
    Dim readLastCell As Long
    Dim readLastCellNameSheet As Long
    Dim billNumber
    Dim SheetName As String
    Dim billNumberNamesheet As Long

    Dim sasitaWorkBookPath As String
    Dim excelFilePath
    Dim sasitaWorkBook As Workbook

    excelFilePath = Application.ActiveWorkbook.Path
    Application.EnableEvents = False

    excelFilePath = Application.ActiveWorkbook.Path
    strFilename = Dir(excelFilePath & "\*xlsm")
    sasitaWorkBookPath = excelFilePath & "\Sasita.xlsm"

    SheetName = "Sasita"
    readLastCell = ThisWorkbook.Sheets("Master").Cells(Rows.Count, 1).End(xlUp).Row + 1000
    readLastCellNameSheet = sasitaWorkBook.Sheets("Sasita").Cells(Rows.Count, 1).End(xlUp).Row + 1000

    For x = 4 To readLastCellNameSheet
        cell = "A" & x
        billNumber = ThisWorkbook.Worksheets("Master").Range(cell).Value

        If Len(billNumber) = 0 Then Exit For

        For N = 4 To readLastCellNameSheet
            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.
    sasitaWorkBook.Close True             ' FALSE - DON'T SAVE THE SOURCE FILE.
    Set sasitaWorkBook = Nothing
    Application.EnableEvents = True

    'MsgBox "Updated Succesully"
End Sub

0 个答案:

没有答案