2011-03-02 4 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 

버튼의 매크로 모드에서 실행하면 실행되지만 실행되면 충돌이 뛰어납니다. 문제가 무엇인지 알 수 없습니다. 어떤 아이디어?

답변

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

관련 문제