2011-03-02 76 views
0

此脚本通过复制隐藏的工作表模板并删除现有工作表(重新填充某些参考数据后)来重置模板。我已经测试过它,它在调试模式下运行良好。子程序完成后Excel VBA崩溃

Option Explicit 
Sub reset_PrintLayout_byCopy() 
    'the script replace the used printlayout with a copy from the hidden master. 

    Dim MeetingData() As String 
    Dim i As Integer 
    Dim j As Integer 
    Dim currentSheet As String 
    Dim datacolumns() As String 
    Dim userConfirm As String 
    ReDim Preserve MeetingData(3, 2) 
    ReDim Preserve datacolumns(2) 

    'warning about deleting data 
    userConfirm = MsgBox(Prompt:="Resetting the template will erase all data on the " _ 
    & "PrintLayout Template. Choose ""Cancel"", if you wish to save the file first", _ 
    Buttons:=vbOKCancel, Title:="Data to be erased!") 

    If (userConfirm = vbCancel) Then 
     Exit Sub 
    End If 

    'set parameters 
    datacolumns(0) = "D1" 
    datacolumns(1) = "I1" 


    'stop screen updating and displaying warnings 
    Application.ScreenUpdating = False 
    Application.DisplayAlerts = False 


    'set active sheet 
    currentSheet = ActiveSheet.Name 

    'capture meeting data already filled out 
    For j = 0 To UBound(datacolumns) - 1 
     For i = 1 To 3 
      If Worksheets(currentSheet).Cells(i, Range(datacolumns(j)).Column).Value <> "" Then 
       MeetingData(i - 1, j) = Worksheets(currentSheet).Cells(i, Range(datacolumns(j)).Column).Value 
      End If 

     Next i 
    Next j 

    'make hidden template visible 
    Worksheets("hiddenPrintLayoutTemplate").Visible = True 

    'Rename current Sheet 
     Sheets(currentSheet).Name = "used_Print_Layout" 

    ''add a new sheet 
    ' ActiveWorkbook.Worksheets.Add(before:=Sheets("used_Print_Layout")).Name = "PrintLayout Template" 

    'copy hiddentemplate before current sheet 
     Worksheets("hiddenPrintLayoutTemplate").Copy before:=Sheets("used_Print_Layout") 
     ActiveSheet.Name = currentSheet 

    'set rowheight for title rows 
     Range("A12").EntireRow.RowHeight = 24 
     Range("A18").EntireRow.RowHeight = 24 

    'delete current used printlayout 
     Worksheets("used_Print_Layout").Delete 

    'refilled meeting data 
    For j = 0 To UBound(datacolumns) - 1 
     For i = 1 To 3 
      If MeetingData(i - 1, j) <> "" Then 
       Worksheets(currentSheet).Cells(i, Range(datacolumns(j)).Column).Value = MeetingData(i - 1, j) 
      End If 
     Next i 
    Next j 

    'hide PrintLayout template 
    'Worksheets("hiddenPrintLayoutTemplate").Visible = xlSheetVeryHidden 
    'Sheets("PrintLayout Template").Select 

    'activate screenupdating and display warnings 
    Application.DisplayAlerts = True 
    Application.ScreenUpdating = True 
End Sub 

当它在按钮上的宏模式下运行时,它运行,但excel崩溃,当它完成。我找不到问题所在。有任何想法吗?

回答

2

我不知道如果通过调试你是指逐行逐行,但你可以尝试在代码中的关键点处插入stop语句。因此,例如,你可以把一个stop声明如下部分:

'capture meeting data already filled out 
For j = 0 To UBound(datacolumns) - 1 
    For i = 1 To 3 
     If Worksheets(currentSheet).Cells(i, Range(datacolumns(j)).Column).Value <> "" Then 
      MeetingData(i - 1, j) = Worksheets(currentSheet).Cells(i,Range(datacolumns(j)).Column).Value 
     End If 
    Next i 
Next j 

stop 

'make hidden template visible 
Worksheets("hiddenPrintLayoutTemplate").Visible = True 

如果代码运行正常,直到这一点你可以看到(即运行而不调试)。如果是这样,请删除stop语句并将其放在代码的更下方。重复这个步骤,直到找到导致崩溃的语句 - 也许原因将会出现。

-1

通常,如果您在Excel VBA中发生奇怪的崩溃,请尝试将Windows默认打印机切换到Microsoft XPS Document Writer。似乎很奇怪,但是这对我来说很困难,因为我浪费了很多时间才发现这是罪魁祸首。

+1

鉴于提交的问题,这看起来非常牵强。 – CmdrSharp