Message 1 of 6
Need help with some code involving Excel

Not applicable
05-08-2008
09:28 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
The following code is the code i have for opening an existing excel spreadsheet, and the other sub after is for putting data into excel. However I continue to get a "Runtime - error "424" Object Required" error for whichever listindex i choose. I have surrounded the code with error in *'s. Is this possibly something to do with my excel open code being wrong? or am i just missing something?
Private Sub OpenExcel_Click()
Dim oExcel As Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
'Open Excel
Set oExcel = New Excel.Application
oExcel.Visible = True
Set oBook = oExcel.Workbooks.Add("\\fscs\Students\curtisno\Desktop\217ExcelCost.xls")
Set oSheet = oBook.Sheets("UnitCosts")
oExcel.DisplayAlerts = False
'Save new file
'-------------
Dim ExcelFileName As String
Dim msg1 As Variant
'open SaveAs dialog
CommonDialog1.ShowSave
'define the name for new file. When Save button is clicked, new file is created and saved.
ExcelFileName = CommonDialog1.FileName
'just the note for user if Cancel button is clicked
If ExcelFileName = "" Then
msg1 = MsgBox("You didn't create a file.", vbOKOnly, "Note")
Exit Sub
End If
'-------------------
'Call oBook.Close(True, "C:\myFile1.xls")
Call oBook.Close(True, ExcelFileName)
oExcel.DisplayAlerts = True
oExcel.Quit
Set oSheet = Nothing
Set oBook = Nothing
Set oExcel = Nothing
End Sub
Private Sub ExteriorGet_Click()
Dim pt1, pt2 As Variant
Dim objPline As AcadLWPolyline
Dim Points() As Double
Dim distanceEXT As Double
Dim areaEXT As Double
UserForm1.Hide
On Error Resume Next
pt1 = ThisDrawing.Utility.GetPoint(, "Enter First Point")
If Err.Number <> 0 Then Exit Sub
ReDim Points(1)
Points(0) = pt1(0): Points(1) = pt1(1)
i = 1
Do
pt2 = ThisDrawing.Utility.GetPoint(pt1, "Enter Next Point")
If Err.Description <> "User input is a keyword" Then
i = i + 2
ReDim Preserve Points(i)
Points(i - 1) = pt2(0)
Points(i) = pt2(1)
objPline.Delete
Set objPline = ThisDrawing.ModelSpace.AddLightWeightPolyline(Points)
objPline.Update
pt1 = pt2
Else
Exit Do
End If
Loop
distanceEXT = objPline.Length
areaEXT = objPline.Area
objPline.Delete
distanceEXTa = distanceEXT
areaEXTa = areaEXT
UserForm1.Show
On Error GoTo 0
If FoundationTypeBox.ListIndex = 0 Then
oSheet.Cells(3, 4).Value = (Worksheet.Cells(3, 4).Value + distanceEXT)
ElseIf FoundationTypeBox.ListIndex = 1 Then
oSheet.Cells(4, 4).Value = (Worksheet.Cells(4, 4).Value + distanceEXT)
ElseIf FoundationTypeBox.ListIndex = 2 Then
oSheet.Cells(5, 4).Value = (Worksheet.Cells(5, 4).Value + distanceEXT)
Else: MsgBox "No Foundation Type Selected."
End If
If FloorSlabBox.ListIndex = 0 Then
oSheet.Cells(7, 4).Value = (Worksheet.Cells(7, 4).Value + areaEXT)
ElseIf FloorSlabBox.ListIndex = 1 Then
oSheet.Cells(8, 4).Value = (Worksheet.Cells(8, 4).Value + areaEXT)
ElseIf FloorSlabBox.ListIndex = 2 Then
oSheet.Cells(9, 4).Value = (Worksheet.Cells(9, 4).Value + areaEXT)
Else: MsgBox "No Floor Slab Selected."
End If
If ExteriorWallBox.ListIndex = 0 Then
oSheet.Cells(11, 4).Value = (Worksheet.Cells(11, 4).Value + distanceEXT)
ElseIf ExteriorWallBox.ListIndex = 1 Then
oSheet.Cells(12, 4).Value = (Worksheet.Cells(12, 4).Value + distanceEXT)
ElseIf ExteriorWallBox.ListIndex = 2 Then
oSheet.Cells(13, 4).Value = (Worksheet.Cells(13, 4).Value + distanceEXT)
Else: MsgBox "No Exterior Wall Selected."
End If
If RoofStructureBox.ListIndex = 0 Then
oSheet.Cells(52, 4).Value = (Worksheet.Cells(52, 4).Value + areaEXT)
ElseIf RoofStructureBox.ListIndex = 1 Then
oSheet.Cells(53, 4).Value = (Worksheet.Cells(53, 4).Value + areaEXT)
Else: MsgBox "No Roof Structure Selected."
End If
If RoofMaterialsBox.ListIndex = 0 Then
oSheet.Cells(55, 4).Value = (Worksheet.Cells(55, 4).Value + areaEXT)
ElseIf RoofMaterialsBox.ListIndex = 1 Then
oSheet.Cells(56, 4).Value = (Worksheet.Cells(56, 4).Value + areaEXT)
ElseIf RoofMaterialsBox.ListIndex = 2 Then
oSheet.Cells(57, 4).Value = (Worksheet.Cells(57, 4).Value + areaEXT)
ElseIf RoofMaterialsBox.ListIndex = 3 Then
oSheet.Cells(58, 4).Value = (Worksheet.Cells(58, 4).Value + areaEXT)
Else: MsgBox "No Roof Materials Selected."
End If
End Sub
Private Sub OpenExcel_Click()
Dim oExcel As Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
'Open Excel
Set oExcel = New Excel.Application
oExcel.Visible = True
Set oBook = oExcel.Workbooks.Add("\\fscs\Students\curtisno\Desktop\217ExcelCost.xls")
Set oSheet = oBook.Sheets("UnitCosts")
oExcel.DisplayAlerts = False
'Save new file
'-------------
Dim ExcelFileName As String
Dim msg1 As Variant
'open SaveAs dialog
CommonDialog1.ShowSave
'define the name for new file. When Save button is clicked, new file is created and saved.
ExcelFileName = CommonDialog1.FileName
'just the note for user if Cancel button is clicked
If ExcelFileName = "" Then
msg1 = MsgBox("You didn't create a file.", vbOKOnly, "Note")
Exit Sub
End If
'-------------------
'Call oBook.Close(True, "C:\myFile1.xls")
Call oBook.Close(True, ExcelFileName)
oExcel.DisplayAlerts = True
oExcel.Quit
Set oSheet = Nothing
Set oBook = Nothing
Set oExcel = Nothing
End Sub
Private Sub ExteriorGet_Click()
Dim pt1, pt2 As Variant
Dim objPline As AcadLWPolyline
Dim Points() As Double
Dim distanceEXT As Double
Dim areaEXT As Double
UserForm1.Hide
On Error Resume Next
pt1 = ThisDrawing.Utility.GetPoint(, "Enter First Point")
If Err.Number <> 0 Then Exit Sub
ReDim Points(1)
Points(0) = pt1(0): Points(1) = pt1(1)
i = 1
Do
pt2 = ThisDrawing.Utility.GetPoint(pt1, "Enter Next Point")
If Err.Description <> "User input is a keyword" Then
i = i + 2
ReDim Preserve Points(i)
Points(i - 1) = pt2(0)
Points(i) = pt2(1)
objPline.Delete
Set objPline = ThisDrawing.ModelSpace.AddLightWeightPolyline(Points)
objPline.Update
pt1 = pt2
Else
Exit Do
End If
Loop
distanceEXT = objPline.Length
areaEXT = objPline.Area
objPline.Delete
distanceEXTa = distanceEXT
areaEXTa = areaEXT
UserForm1.Show
On Error GoTo 0
If FoundationTypeBox.ListIndex = 0 Then
oSheet.Cells(3, 4).Value = (Worksheet.Cells(3, 4).Value + distanceEXT)
ElseIf FoundationTypeBox.ListIndex = 1 Then
oSheet.Cells(4, 4).Value = (Worksheet.Cells(4, 4).Value + distanceEXT)
ElseIf FoundationTypeBox.ListIndex = 2 Then
oSheet.Cells(5, 4).Value = (Worksheet.Cells(5, 4).Value + distanceEXT)
Else: MsgBox "No Foundation Type Selected."
End If
If FloorSlabBox.ListIndex = 0 Then
oSheet.Cells(7, 4).Value = (Worksheet.Cells(7, 4).Value + areaEXT)
ElseIf FloorSlabBox.ListIndex = 1 Then
oSheet.Cells(8, 4).Value = (Worksheet.Cells(8, 4).Value + areaEXT)
ElseIf FloorSlabBox.ListIndex = 2 Then
oSheet.Cells(9, 4).Value = (Worksheet.Cells(9, 4).Value + areaEXT)
Else: MsgBox "No Floor Slab Selected."
End If
If ExteriorWallBox.ListIndex = 0 Then
oSheet.Cells(11, 4).Value = (Worksheet.Cells(11, 4).Value + distanceEXT)
ElseIf ExteriorWallBox.ListIndex = 1 Then
oSheet.Cells(12, 4).Value = (Worksheet.Cells(12, 4).Value + distanceEXT)
ElseIf ExteriorWallBox.ListIndex = 2 Then
oSheet.Cells(13, 4).Value = (Worksheet.Cells(13, 4).Value + distanceEXT)
Else: MsgBox "No Exterior Wall Selected."
End If
If RoofStructureBox.ListIndex = 0 Then
oSheet.Cells(52, 4).Value = (Worksheet.Cells(52, 4).Value + areaEXT)
ElseIf RoofStructureBox.ListIndex = 1 Then
oSheet.Cells(53, 4).Value = (Worksheet.Cells(53, 4).Value + areaEXT)
Else: MsgBox "No Roof Structure Selected."
End If
If RoofMaterialsBox.ListIndex = 0 Then
oSheet.Cells(55, 4).Value = (Worksheet.Cells(55, 4).Value + areaEXT)
ElseIf RoofMaterialsBox.ListIndex = 1 Then
oSheet.Cells(56, 4).Value = (Worksheet.Cells(56, 4).Value + areaEXT)
ElseIf RoofMaterialsBox.ListIndex = 2 Then
oSheet.Cells(57, 4).Value = (Worksheet.Cells(57, 4).Value + areaEXT)
ElseIf RoofMaterialsBox.ListIndex = 3 Then
oSheet.Cells(58, 4).Value = (Worksheet.Cells(58, 4).Value + areaEXT)
Else: MsgBox "No Roof Materials Selected."
End If
End Sub