我有一个宏来复制选定的单元格并将其粘贴到特定格式,它基本上是一个带分号分隔符的坐标(东向和北向)列表,然后将其保存到记事本中。
之后我的宏打开autocad并调用我在autocad中写入的lisp,将记事本导入到描述点。 我使用excel和autocad 2007。
我的宏按预期工作。但是我有一点刺激性问题。这是在我运行宏并且在autocad上绘制点之后,如果我通过在“ABCD ...”栏中选择突出显示来删除excel中的列(例如,我通过突出显示I到F将列F删除到列I bar),然后单击autocad工具栏中的“文件”或“编辑”。 excel会崩溃。它会自动关闭,然后在恢复模式下再次打开。
当我点击“文件”或“编辑”时,鼠标光标会显示“思考”动画一秒钟。然后excel就崩溃了。 经过多次测试后,解决方法是在excel中执行操作。例如,如果我在删除列后复制并粘贴一个随机单元格,当我单击“文件”或“编辑”时,它不会使excel崩溃。我猜这是我编写代码的方式和选择相关的问题。 只是我希望这些代码也被我的朋友们使用,我希望它没有bug。
我想知道我的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 :(得分:0)
我不相信你的陈述found = None
for line_split in f:
if search in line_split:
...
if not found:
found = True
if not found:
print ("You searched for %s" % search)
print ("Match Not Found.")
是一个“有效”的陈述。虽然它似乎在您的代码中运行,但我担心这可能会导致内存中发生奇怪的事情。
假设Set ACAD = AcadApplication
是对象类型,AcadApplication
有效,并将变量ACAD设置为Set ACAD = New AcadApplication
类型的新对象。
ACADApplication
有效,并告诉编译器ACAD将用作Dim ACAD As AcadApplication
对象。
但是我不确定如果将ACADApplication
变量设置为对象类型会发生什么。似乎编译器甚至不应该允许它发生。
由于您立即将ACAD
设置为其他内容(由您的ACAD
或Set ACAD = GetObject(, "AutoCAD.Application")
设置),因此删除Set ACAD = New AcadApplication
一行应该是安全的。< / p>
但我之前从未遇到Set ACAD = AcadApplication
对象,因此您可能需要参考其文档,以确保它没有以某种不寻常的方式设计。