简体   繁体   中英

VBA Workbook crashes/closes after running subroutine

I have a VBA code that is meant to help me track the time I spend working on a project and on the specific tasks. I've created a custom form that way no matter what tab I'm on, I can convenient input my tab with a shortcut key. Here's a picture of the form: Custom Form

Once you hit submit, the code should just take all the information in the code and add it to a specific spreadsheet. This is where I bump into one of two different issues:

  1. after 1 or two submissions, I'll get a mismatch error on a variable even though the previous submissions did not have an error. When I attempt to debug the code, Excel closes and tells me that the code is corrupted after I reopen it.

  2. I'll click or try to change something in the spreadsheet and excel will close without any warnings and won't even inform me if the code is corrupted.

I have option explicit on, I believe there are no variant type variables. I've copied and pasted all the code to another spreadsheet and get the same problem. Can someone please tell me what's wrong with the spreadsheet? Here's the code linked with the codes submit button.

Private Sub SubmitButton_Click()

Dim EditIndex As Integer
EditIndex = ProjectTaskLog.IndexInput.Value
If EditIndex > 1 Then
    Call EditEntry
Call ProjectTaskLogForm.EditEntry
Else
    Call ProjectTaskLogForm.PTSubmit
End If

Here's the code associated with submitting the form values to a spreadsheet:

Sub PTSubmit()

Dim Agenda As Worksheet
Set Agenda = ThisWorkbook.Worksheets("agenda")

Dim Submission As PTLog
Set Submission = New PTLog
With Submission
    .Project = ProjectTaskLog.ProjectCmb.Value
    .OrderNumber = ProjectTaskLog.OrderCmb.Value
    .Task = ProjectTaskLog.TaskCmb.Value
    .Detail = ProjectTaskLog.DetailInput
    .StartT = ProjectTaskLog.StartInput.Value
    .EndT = ProjectTaskLog.EndInput.Value
    .Hours = ProjectTaskLog.HoursInput.Value
    '.SDate = ProjectTaskLog.DateInput.Value
    .OTStatus = ProjectTaskLog.OTSInput.Value
    .Overtime = ProjectTaskLog.OvertimeInput.Value
    If .Overtime > 0 Then .Hours = .Hours - .Overtime
End With

Dim IRow As Long
IRow = Agenda.Range("c1").End(xlDown).Row + 1
Dim AgendaArr(1 To 1, 1 To 9) As String
        AgendaArr(1, 1) = Date
        AgendaArr(1, 3) = Submission.OrderNumber
        AgendaArr(1, 4) = Submission.Project
        AgendaArr(1, 5) = Submission.Task
        AgendaArr(1, 6) = Submission.Detail
        AgendaArr(1, 2) = "NO"
        AgendaArr(1, 7) = Submission.Hours
        AgendaArr(1, 8) = Submission.StartT
        AgendaArr(1, 9) = Submission.EndT
        Agenda.Range("c" & IRow, "k" & IRow) = AgendaArr
If Submission.Overtime > 0 Then
    IRow = IRow + 1

    AgendaArr(1, 1) = Format(Submission.SDate, "m/dd/yyyy")
    AgendaArr(1, 2) = "YES"
    AgendaArr(1, 3) = Submission.OrderNumber
    AgendaArr(1, 4) = Submission.Project
    AgendaArr(1, 5) = Submission.Task
    AgendaArr(1, 6) = Submission.Detail
    AgendaArr(1, 7) = Submission.Overtime
    AgendaArr(1, 8) = Submission.StartT
    AgendaArr(1, 9) = Submission.EndT
    Agenda.Range("c" & IRow, "k" & IRow) = AgendaArr
End If

End Sub
Dim AgendaArr(1 To 1, 1 To 9) As String

Some of the values you assign are not strings and you get type mismatch error as a result. Change the declaration type to Variant.

You could eliminate the array by using methods on the PTLog object to save values from the form to the sheet and that should avoid type mismatch errors.

Sub PTSubmit()
    
    Dim p As New PTLog
    Call p.Init(Me)
    Call p.Save(ThisWorkbook.Sheets("agenda"))

End Sub

PTLog Class Module

Public Project As String
Public OrderNumber As String, Task As String, Detail As String
Public StartT As String, EndT As String, sDate As Date
Public OTStatus As String
Public Hours As Single, Overtime As Single

' initilize object
Function Init(frm As Object)
    With frm
        Project = .ProjectCmb.Value
        OrderNumber = .OrderCmb.Value
        Task = .TaskCmb.Value
        Detail = .DetailInput
        StartT = .StartInput.Value
        EndT = .EndInput.Value
        Hours = Val(.HoursInput.Value)
        sDate = Format(.DateInput.Value, "m/dd/yyyy")
        OTStatus = .OTSInput.Value
        Overtime = Val(.OvertimeInput.Value)
   End With
   If Overtime > 0 Then Hours = Hours - Overtime
End Function

' save object to sheet
Function Save(ws As Worksheet)
    Dim i As Long, z As Integer, n As Integer
    i = ws.Cells(rows.Count, 3).End(xlUp).Row + 1

    z = 1
    If Overtime > 0 Then z = 2

    For n = 1 To z
        With ws
            .Cells(i, "C") = Format(sDate, "m/dd/yyyy")
            If n = 1 Then
                .Cells(i, "D") = "NO"
                .Cells(i, "I") = Hours
            Else
                .Cells(i, "D") = "YES"
                .Cells(i, "I") = Overtime
            End If
            .Cells(i, "E") = OrderNumber
            .Cells(i, "F") = Project
            .Cells(i, "G") = Task
            .Cells(i, "H") = Detail
            .Cells(i, "J") = StartT
            .Cells(i, "K") = EndT
        End With
        i = i + 1
    Next

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM