i have an autocad userform that lists all the layout tabs in a drawing and allows the user to select which ones to keep and deletes the rest. I am trying to coordinate with excel so autocad opens excel then opens the appropriate workbook and deletes the same tabs in excel.
I have almost everything working but i can not get the selected sheets to delete. Below is my code the problem i think is in the section just above the full code.
Code:
If excel.Workbooks(file).Worksheets.Count > UBound(MyArray) Then
excel.DisplayAlerts = False
'On Error Resume Next
On Error GoTo 0
excel.Workbooks(file).Worksheets(MyArray).Delete
excel.DisplayAlerts = True
'Call UpdateSheetList
Else
MsgBox "A workbook must contain at least one visible sheet.", vbExclamation
End If
Code:
Private Sub SubmitButton_Click()
'deletes unselected items to leave only the drawing needed
Dim i As Long, pth, file, xlFile, MyArray() As Variant, Cnt As Long
Dim excel As Object, xlWkSht As Object
file = ThisDrawing.name
pth = ThisDrawing.Path & "\"
file = Left(ThisDrawing.name, (Len(file) - 4)) & ".xls"
'On Error GoTo 0
On Error Resume Next
Set excel = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set excel = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "Could Not Load Excel!", vbExclamation
End If
End If
excel.Visible = True
Set xlFile = excel.Workbooks.Open(pth & file)
'*resume error handling
'On Error GoTo 0
'*DELETE XL SHEETS
With Me.ListBox1
Cnt = 0
For i = 0 To .ListCount - 1
If .Selected(i) = False Then
Cnt = Cnt + 1
ReDim Preserve MyArray(1 To Cnt)
MyArray(Cnt) = .List(i)
End If
Next i
If Cnt > 0 Then
If excel.Workbooks(file).Worksheets.Count > UBound(MyArray) Then
excel.DisplayAlerts = False
'On Error Resume Next
On Error GoTo 0
excel.Workbooks(file).Worksheets(MyArray).Delete
excel.DisplayAlerts = True
'Call UpdateSheetList
Else
MsgBox "A workbook must contain at least one visible sheet.", vbExclamation
End If
Else
MsgBox "Please select one or more sheets for deletion...", vbExclamation
End If
End With
'*DELETE AUTOCAD LAYOUTS
With Me.ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) = False Then
ThisDrawing.Layouts.Item(ListBox1.List(i, 0)).Delete
End If
Next i
End With
call Format
Unload Me
End Sub
if someone would have a look i would be forever grateful!