Excel宏崩溃

时间:2016-01-26 15:54:54

标签: excel vba excel-vba

我正在尝试从加载项中获取数据,并根据输入将工作簿保存为值。我将输入放在一个数组中,并尝试将它们一次放入一个,只将值保存到另一个文件中,然后移动到下一个项目。我必须为可能数百个输入执行此操作,以便使用宏。加载项需要一点时间才能运行所以我有延迟子,但经过几次运行后,excel只是崩溃而没有任何警告。你们有什么想法可能会发生什么吗?

Sub loopThru()
Dim ws As Worksheet
Dim Wb As Workbook
Set ws = Worksheets("Sheet 1")
Set Wb = ActiveWorkbook

For i = 16 To 22
    Wb.Activate
    ws.Activate
    Call delay(60)
    ws.Range("B2").Value = ws.Range("A" & i).Value
    Call delay(60)
    Call saveValues
    Wb.Activate
Next i

End Sub

Private Sub delay(seconds As Long)
    Dim endTime As Date
    endTime = DateAdd("s", seconds, Now())
    Do While Now() < endTime
        DoEvents
    Loop
End Sub

Sub saveValues()
    Dim ws As Worksheet
    Dim fileName As String
    Dim Wb As Workbook
    Set Wb = ActiveWorkbook
    Sheets("Portfolio Information").Select
    fileName = "C:\My Documents\Text\" & Range("B2").Text & "\File" & Range("B2").Text & ".xls"

    ThisWorkbook.SaveCopyAs fileName

    Workbooks.Open fileName

    Set Wb1 = ActiveWorkbook

For Each ws In ActiveWorkbook.Worksheets
        ws.Cells.Copy
        ws.Cells.PasteSpecial xlPasteValues
    Next
Application.CutCopyMode = False
    Wb1.Save
    Wb1.Close

    Wb.Activate

End Sub

0 个答案:

没有答案