Quantcast
Channel: MicroStation Programming - Forum - Recent Threads
Viewing all articles
Browse latest Browse all 1677

[v8i VBA] Error in finding last row in Excel using VBA

$
0
0

Hi All,

I think this is bit of Excel related question than mvba...but any help on this will help me.

what I want to do is create error report in an Excel sheet using VBA. below is the sample code which creates excel in a given path, but I'm getting error while adding data to the created xls. below I have Highlighted the code where I 'm getting error. please suggest me.

Option Explicit
Private Sub CommandButton2_Click()
Dim Excel As Object
Dim book As Object
Dim sheet As Excel.Worksheet
Dim fName As String
Dim oSheet As String
Dim oSheet1 As String
'Dim xlApp As New Excel.Application

With CDialog1
'.CancelError = True
.Filter = "Microsoft Excel (*.XLS)|*.XLS"
.FilterIndex = 1
.DialogTitle = "Save File"
.FileName = TextBox3.Text
.ShowSave

End With
fName = CDialog1.FileName
Dim strFile As String
strFile = (TextBox3.Value)
If Len(strFile) = 0 Then Exit Sub
'Set Excel = CreateObject("Excel.Application")
'Set book = Excel.Workbooks.Add
Dim DirFile As String
DirFile = fName
If Len(Dir(DirFile)) = 0 Then
Set Excel = CreateObject("Excel.Application")
Set book = Excel.Workbooks.Add
oSheet = TextBox1.Text
oSheet = VBA.UCase(oSheet)
Set sheet = book.Worksheets.Add
sheet.Name = oSheet

' create headers in xls
sheet.Cells(1, 1).Value = "Date & Time"
sheet.Cells(1, 2).Value = "Project Name"
sheet.Cells(1, 3).Value = "Delivery Name"
sheet.Cells(1, 4).Value = "Stage No"
sheet.Cells(1, 5).Value = "QA Staff Name"
sheet.Cells(1, 6).Value = "Prod. Staff Name"
sheet.Cells(1, 7).Value = "DGN Name"
sheet.Cells(1, 8).Value = "Edge Match"
sheet.Cells(1, 9).Value = "Wrong Layer"
sheet.Cells(1, 10).Value = "Others"
sheet.Cells(1, 11).Value = "Vertcheck"
sheet.Cells(1, 12).Value = "Missing"
sheet.Cells(1, 13).Value = "Interpretation"
sheet.Cells(1, 14).Value = "XY Position"
sheet.Cells(1, 15).Value = "Z Position"
sheet.Cells(1, 16).Value = "Delete"
sheet.Cells(1, 17).Value = "% of Errors"
sheet.Cells(1, 18).Value = "Comments"
book.SaveAs fName
book.Close
Excel.Quit
Set book = Nothing
Set Excel = Nothing
' add data to xls

Dim lastRow As Long
Set Excel = CreateObject("Excel.Application")
Set book = Excel.Workbooks.Open(DirFile)
With book.Worksheets(oSheet)
Set sheet = book.Sheets(oSheet)

lastRow = sheet.Range("A" & sheet.Rows.Count).End(Excel.xlUp).Row + 1 "Here I am getting object doesn't support this property or Method"

sheet.Range("Date & Time" & lastRow) = VBA.Date$
sheet.Range("Project Name" & lastRow) = TextBox1.Text
sheet.Range("Delivery Name" & lastRow) = TextBox2.Text
sheet.Range("Stage No" & lastRow) = "2"
sheet.Range("QA Staff Name" & lastRow) = TextBox4.Text
sheet.Range("Prod. Staff Name" & lastRow) = TextBox3.Text
sheet.Range("DGN Name" & lastRow) = "STRIP-1"
sheet.Range("Edge Match" & lastRow) = "1"
sheet.Range("Wrong Layer" & lastRow) = "1"
sheet.Range("Others" & lastRow) = "1"
sheet.Range("Vertcheck" & lastRow) = "1"
sheet.Range("Missing" & lastRow) = "1"
sheet.Range("Interpretation" & lastRow) = "1"
sheet.Range("XY Position" & lastRow) = "1"
sheet.Range("Z Position" & lastRow) = "1"
sheet.Range("Delete" & lastRow) = "1"
sheet.Range("% of Errors" & lastRow) = "35%"
sheet.Range("Comments" & lastRow) = TextBox5.Text

End With

If MsgBox("One record written to oSheet. Do you want to continue entering data?", vbYesNo + vbCritical, "Caution") = vbYes Then
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox1.SetFocus
Else
Unload Me
End If
book.Save
Excel.Quit
Set book = Nothing
Set Excel = Nothing
End if
End Sub
Best regards,
Prasanna 

Viewing all articles
Browse latest Browse all 1677

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>