The Form code below was working correctly, the all of a sudden, it has stopped putting…
- User
The Form code below was working correctly, the all of a sudden, it has stopped putting the List values in the right places. When I click on the submit button the first list value (.Cells(lastRow, 1).Value = desc) is submited to the correct columns A. The second list value (.Cells(lastRow, 2).Value = Sheets("List").Cells(TaskType, 1).Value) in colum B is not what I entered in the form The third list value (.Cells(lastRow, 3).Value = createDate) in column C is correctly placed, The fourth list value (.Cells(lastRow, 4).Value = Sheets("List").Cells(Collaborators, 1).Value) in column D is in the wrong place The fifth list value is (.Cells(lastRow, 5).Value = Sheets("List").Cells(Resources, 1).Value) duplicated in column E and in column F The sixth list value is (.Cells(lastRow, 6).Value = Sheets("List").Cells(priority, 1).Value) is a duplication of what is entered in column E The seventh list value (.Cells(lastRow, 7).Value = compDate) in column G is correctly placed and the eigth list value (.Cells(lastRow, 8).Value = Sheets("List").Cells(stages, 1).Value) in column H is bot what I entered in the form. Can you detect and correct what is causing this problem.
Private Sub CommandButton1_Click() Application.EnableEvents = False ' Get data from form Dim desc As String: desc = Me.TaskDescription.Value Dim TaskType As Long: TaskType = Me.TaskType.ListIndex + 1 ' Adjust for 0-based indexing Dim createDate As Date: createDate = Me.DateCreated.Value Dim Collaborators As Long: Collaborators = Me.Collaborators.ListIndex + 1 ' Adjust for 0-based indexing Dim Resources As Long: Resources = Me.Resources.ListIndex + 1 ' Adjust for 0-based indexing Dim priority As Long: priority = Me.TaskPriority.ListIndex + 1 ' Adjust for 0-based indexing Dim compDate As Date: compDate = Me.CompletionDate.Value Dim stages As Long: stages = Me.StagesRequired.ListIndex + 1 ' Adjust for 0-based indexing
' Calculate difference in days Dim diffDays As Long: diffDays = DateDiff("d", Date, compDate)
' Determine target sheet based on difference in days Dim targetSheet As String Select Case diffDays Case Is <= 3: targetSheet = "Day" Case 4 To 9: targetSheet = "Week" Case 10 To 20: targetSheet = "Forthnight" Case 21 To 59: targetSheet = "Month" Case 60 To 89: targetSheet = "QtrYear" Case 90 To 209: targetSheet = "HalfYear" Case Else: targetSheet = "Year" End Select
' Find next available row Dim lastRow As Long: lastRow = Sheets(targetSheet).Cells(Rows.Count, 1).End(xlUp).Row + 1
' Write data to sheet With Sheets(targetSheet) .Cells(lastRow, 1).Value = desc .Cells(lastRow, 2).Value = Sheets("List").Cells(TaskType, 1).Value .Cells(lastRow, 3).Value = createDate .Cells(lastRow, 4).Value = Sheets("List").Cells(Collaborators, 1).Value .Cells(lastRow, 5).Value = Sheets("List").Cells(Resources, 1).Value .Cells(lastRow, 6).Value = Sheets("List").Cells(priority, 1).Value .Cells(lastRow, 7).Value = compDate .Cells(lastRow, 8).Value = Sheets("List").Cells(stages, 1).Value End With
' Display confirmation message MsgBox "Your new task has been recorded in " & targetSheet & ".", vbInformation
- Assistant
Based on the provided code, there are a few potential issues that could be causing the problem:
- User
I have nade your corrections but the output is still wrong
- Assistant
If the output is still incorrect after making the suggested corrections, there may be an issue with the data source in the "List" sheet. Here are a few steps you can take to troubleshoot and further investigate the problem:
-
Verify the column references in the code:
- Confirm that the column references in the code accurately match the column positions in the "List" sheet.
-
- User
error: Application-defined or object-defined error on the line: .Cells(lastRow, 8).Value = Sheets("List").Cells(stages, 1).Value
- Assistant
The "Application-defined or object-defined error" typically occurs when there is an issue with the range or cell reference being used. In this case, the error is being thrown on the line
.Cells(lastRow, 8).Value = Sheets("List").Cells(stages, 1).Value.One possible cause of this error could be that the variable
stagesis not correctly referring to the index value of the selected item in the "StagesRequired" list box. Please ensure that you have made the necessary changes according to the previous suggestions and that the variable is correctly referring to the index value.To troubleshoot this further, you can add some debug lines to check the value of the
stagesvariable before this line to ensure it is set correctly. For example, add the following line before theWithstatement:vbaDebug.Print stagesCheck the output in the Immediate window to see if the value of
stagesis as expected. If it is not the correct value, there may be an issue with how the index is determined or assigned to thestagesvariable.If you could provide a sample of the data in the "List" sheet and specify the expected output, it would be helpful in identifying the exact cause of the error and providing a more accurate solution.
- User
The data in the sheet lis is as follows
Task Type, Collaborators, Resources Required, Priority, Stages Required, End Day To Day, None, Self, High, Start Execute Finish, Not Started ADHOC, PremisesStaff, PremisesStaff, Medium, Start Plan Execute Finish, Completed Time Bound, OtherStaff, OtherStaff, Low, Start Collaborate Plan Execute Finish, On Hold Health&Safety, Management, Contractors, , , In Progress Legislative, Contractors, , , , Suspended Other, OtherThirdParty , , , Transferred , , , , , Cancelled
- Assistant
Based on the provided data in the "List" sheet, there are a few potential issues that could cause the error on the line
.Cells(lastRow, 8).Value = Sheets("List").Cells(stages, 1).Value. Here are some possible scenarios and suggestions for troubleshooting: - User
I have added values to all the empty cells in the list columns so that all list columns have values in row 2 to row 8. The code is still working incorrectly
- Assistant