2016-09-29 34 views
0

我有一個宏來複制選定的單元格並將其粘貼到特定的格式,它基本上是一個帶分號分隔符的座標(東行和北行)列表,然後將其保存到記事本。運行宏與autocad一起工作後刪除列後崩潰Excel

之後,我的宏打開autocad並調用我在autocad中寫入的lisp,將記事本導入到描述中。 我使用excel和autocad 2007.

我的宏按預期工作。不過,我有小的刺激性問題。這是我運行宏後,點在autocad上繪製,如果我通過在「ABCD ...。」欄中突出顯示來選擇在Excel中刪除列(例如,我通過突出顯示I到F來刪除列F到列I吧),然後點擊autocad工具欄中的「文件」或「編輯」。 Excel會崩潰。它會自行關閉,然後在恢復模式下再次打開。

當我點擊「文件」或「編輯」時,鼠標光標顯示第二個「思考」動畫。然後,excel剛剛墜毀。 經過多次測試,解決方法是在Excel中執行一個操作。例如,如果我在刪除列後複製並粘貼一個隨機單元格,當單擊「文件」或「編輯」時,它不會使Excel崩潰。我猜這是我寫代碼的方式以及與選擇相關的問題。 這只是我想讓這些代碼也被我的朋友使用,我希望它沒有錯誤。

我不知道是否有什麼我可以改進我的vba代碼?我嘗試了很多我能想到的方法,但無濟於事。 我希望有一個人有類似的問題,知道解決的辦法還是有人可以告訴我這個問題

'This script is to draw point with description in the current active autocad drawing 
'Selected cells will be concatenated to Navipac wp2 format and then saved in L:\Plot to CAD\XLtoCAD.wp2 
'And then it will open autocad or if autocad already opened will tell the cad to type command "wew", which is custom LISP to draw point from wp2 

format file 
'Point will be saved in layer "Point", and description will be saved in layer "Description" with magenta color 
'Agung Hutomo 2016 

'Public ACAD As Object 
'sub to work with cad is from howtoautocad.com/excel-autocad--a-match-made-in-heaven-again/ 
Sub open_Cad() 
Dim warning As Integer 

'ENSURES USER KNOWS OPEN DRAWING WILL BE EDITED, YES NO TO PROCEED 
warning = MsgBox("Selected Coordinates will be plotted into currently opened drawing." & vbCrLf & "Would you like to continue?", vbYesNo, "Data 

Loss Warning") 

Select Case warning 
'if they select yes to proceed 
Case 6 
    Set ACAD = AcadApplication 'Create ACAD variable of type AcadApplication 
    On Error Resume Next 'This tells VBA to ignore errors 
    Set ACAD = GetObject(, "AutoCAD.Application") 'Get a running instance of the class AutoCAD.Application 
    On Error GoTo 0 'This tells VBA to go back to NOT ignoring errors 
    If ACAD Is Nothing Then 'Check to see if the above worked 
     Set ACAD = New AcadApplication 'Set the ACAD variable to equal a new instance of AutoCAD 
     ACAD.Visible = True 'Once loaded, set AutoCAD® to be visible 
    End If 
Case 7 
    Exit Sub 
End Select 

ACAD.ActiveDocument.SendCommand ("wew ") 'Print a message to the AutoCAD® command line 
ACAD.ActiveDocument.SendCommand ("regen ") 

'Inform the user that the drawing was created. 
MsgBox "The coordinates was successfully exported to Autocad!", vbInformation, "Finished" 

End Sub 
'Sub Talk_CAD() 

'End Sub 
Sub concatwptocad() 

'concatenate selected desc, e, n into wp2 format 

    Dim ActSheet As Worksheet 
    Dim SelRange As Range 
    Dim warn As Integer 

    Set ActSheet = ActiveSheet 
    Set SelRange = Selection 

    ' Turn off screen updating. 
    Application.ScreenUpdating = False 

    ActSheet.Select 
    SelRange.Select 
    Selection.Copy 

    Sheets.Add After:=ActSheet 
    Range("A1").Select 
    ActiveSheet.Paste 
    Range("D1").Select 
    Application.CutCopyMode = False 

    If Range("A2") = vbNullString Then 
    ActiveCell.FormulaR1C1 = "=CONCATENATE(char(34),RC[-3],char(34),char(59),RC[-2],char(59),RC[-1],char(59),""0.000"",char(59),14.1,char 

(59),4.1,char(59),14.1,char(59),char(34),""Arial"",char(34),char(59),""0.00"",char(59),-2.1,char(59),char(34),char(34),char(59),""0.00"",char 

(59),char(34),char(34),char(59),1,char(59),""0.000"",char(59),""0.000"",char(59),""0.000"",char(59),0,char(59),0.05)" 

    Else 

    ActiveCell.FormulaR1C1 = "=CONCATENATE(char(34),RC[-3],char(34),char(59),RC[-2],char(59),RC[-1],char(59),""0.000"",char(59),14.1,char 

(59),4.1,char(59),14.1,char(59),char(34),""Arial"",char(34),char(59),""0.00"",char(59),-2.1,char(59),char(34),char(34),char(59),""0.00"",char 

(59),char(34),char(34),char(59),1,char(59),""0.000"",char(59),""0.000"",char(59),""0.000"",char(59),0,char(59),0.05)" 

    Range("D1").Select 
    Selection.AutoFill Destination:=Range("D1:D" & Range("A" & Rows.Count).End(xlUp).Row) 

    End If 

    With Application 

    If Range("A2") = vbNullString Then 
    Range("D1").Select 
    Selection.Copy 

    Else 

    Range(Selection, Selection.End(xlDown)).Select 
    Selection.Copy 
    Application.CutCopyMode = False 

    End If 
    Call Copytonotepad 
    End With 

    Call delsh 


    ActSheet.Activate 

    Call open_Cad 
    'Call Talk_CAD 


    ' Turn off screen updating. 
    Application.ScreenUpdating = True 
    Set SelRange = Nothing 
    Set ActSheet = Nothing 

End Sub 

Private Sub Copytonotepad() 

    Dim f As Integer, c As Range 
    f = FreeFile 
    Open "L:\Plot to CAD\XLtoCAD.wp2" For Output As #f 
    For Each c In Selection 
     Print #f, Replace(c.Value, vbLf, vbCrLf) 
    Next c 
    Close #f 

    Exit Sub 

End Sub 
+0

嗨!所以我評論了set acad = acadapplication,然後再次導致崩潰的序列。而且這次沒有崩潰!非常感謝! –

+0

我在調用copytonotepad之前也註釋掉了selection.copy。我剛剛意識到copytonotepad例程不會從複製的單元格粘貼。 –

+0

我已將我的評論轉貼爲「答案」。 (評論有時會自動刪除,但「答案」更持久。) – YowE3K

回答

0

我不相信你的聲明說Set ACAD = AcadApplication是一個「有效」的語句。雖然它似乎在你的代碼中運行,但我擔心這可能會導致內存中發生奇怪的事情。

假設AcadApplication是一個對象類型,Set ACAD = New AcadApplication是有效的,並將變量ACAD設置爲ACADApplication類型的新對象。

Dim ACAD As AcadApplication有效,並告知編譯器ACAD將用作ACADApplication對象。

但我不確定如果將ACAD變量設置爲對象類型會發生什麼情況。它只是好像編譯器甚至不應該讓它發生。

因爲你立即成立ACAD到別的東西(無論是你的Set ACAD = GetObject(, "AutoCAD.Application"),或者您Set ACAD = New AcadApplication)它應該是安全的刪除行說Set ACAD = AcadApplication

但是我之前從未遇到過AcadApplication這個對象,因此您可能需要查閱它的文檔,以確保它不是以某種不尋常的方式設計的。