In the VBA code below the following lines Case Is >= 4 And Is <= 9: targetSheet = "Week"…
- User
In the VBA code below the following lines Case Is >= 4 And Is <= 9: targetSheet = "Week" Case Is >= 10 And Is <= 20: targetSheet = "Forthnight" Case Is >= 21 And Is <= 59: targetSheet = "Month" Case Is >= 60 And Is <= 89: targetSheet = "QtrYear" Case Is >= 90 And Is <= 209: targetSheet = "HalfYear" have synatax errors can you please correct this.
Private Sub CommandButton1_Click() ' Get data from form Dim desc As String: desc = Me.TaskDescription.Value Dim TaskType As String: TaskType = Me.TaskType.ListIndex + 1 ' Adjust for 0-based indexing Dim Collaborators As String: Collaborators = Me.Collaborators.ListIndex + 1 ' Adjust for 0-based indexing Dim Resources As String: Resources = Me.Resources.ListIndex + 1 ' Adjust for 0-based indexing Dim priority As String: priority = Me.TaskPriority.ListIndex + 1 ' Adjust for 0-based indexing Dim compDate As Date: compDate = Me.CompletionDate.Value Dim stages As String: 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 Is >= 4 And Is <= 9: targetSheet = "Week" Case Is >= 10 And Is <= 20: targetSheet = "Forthnight" Case Is >= 21 And Is <= 59: targetSheet = "Month" Case Is >= 60 And Is <= 89: targetSheet = "QtrYear" Case Is >= 90 And Is <= 209: targetSheet = "HalfYear" Case Else: targetSheet = "Year" End Select
' Find next available row Dim lastRow As Long: lastRow = Sheets(targetSheet).Cells.Find("*", SearchOrder = xlByRows, SearchDirection = xlPrevious).Row + 1
' Write data to sheet Sheets(targetSheet).Cells(lastRow, 1).Value = desc Sheets(targetSheet).Cells(lastRow, 2).Value = Sheets("List").Cells(TaskType, 1).Value Sheets(targetSheet).Cells(lastRow, 3).Value = Me.TaskDescription Sheets(targetSheet).Cells(lastRow, 4).Value = Sheets("List").Cells(Collaborators, 1).Value Sheets(targetSheet).Cells(lastRow, 5).Value = Sheets("List").Cells(Resources, 1).Value Sheets(targetSheet).Cells(lastRow, 6).Value = Sheets("List").Cells(priority, 1).Value Sheets(targetSheet).Cells(lastRow, 7).Value = compDate Sheets(targetSheet).Cells(lastRow, 8).Value = Sheets("List").Cells(stages, 1).Value
' Display confirmation message MsgBox "Your new task has been recorded in " & targetSheet & ".", vbInformation
' Clear form Me.TaskDescription.Value = "" Me.TaskType.ListIndex = -1 ' Set to no selection Me.Collaborators.ListIndex = -1 Me.Resources.ListIndex = -1 Me.TaskPriority.ListIndex = -1 Me.CompletionDate.Value = "" Me.StagesRequired.ListIndex = -1
End Sub
- Assistant
The syntax errors in the code are in the lines:
Case Is >= 4 And Is <= 9: targetSheet = "Week" Case Is >= 10 And Is <= 20: targetSheet = "Forthnight" Case Is >= 21 And Is <= 59: targetSheet = "Month" Case Is >= 60 And Is <= 89: targetSheet = "QtrYear" Case Is >= 90 And Is <= 209: targetSheet = "HalfYear"
The correct syntax should be:
Case Is >= 4 And Is <= 9 targetSheet = "Week" Case Is >= 10 And Is <= 20 targetSheet = "Forthnight" Case Is >= 21 And Is <= 59 targetSheet = "Month" Case Is >= 60 And Is <= 89 targetSheet = "QtrYear" Case Is >= 90 And Is <= 209 targetSheet = "HalfYear"
- User
It still has syntax errors
- Assistant
Apologies for the confusion. The correct syntax should be:
Case Is >= 4 And Is <= 9 targetSheet = "Week" Case Is >= 10 And Is <= 20 targetSheet = "Forthnight" Case Is >= 21 And Is <= 59 targetSheet = "Month" Case Is >= 60 And Is <= 89 targetSheet = "QtrYear" Case Is >= 90 And Is <= 209 targetSheet = "HalfYear"
Please note that in VBA, the "Select Case" statement does not support combining the comparison operators in the same line. Each line should contain only one comparison operator.
- User
when I enter the lines one at a time I get an error Expected: expression on the 'Is' just after the And
- Assistant
Apologies once again for the confusion. The correct syntax should be:
Check if the value is greater than or equal to 4 and less than or equal to 9:
Case Is >= 4 And Is <= 9 targetSheet = "Week"
Check if the value is greater than or equal to 10 and less than or equal to 20:
Case Is >= 10 And Is <= 20 targetSheet = "Forthnight"
Check if the value is greater than or equal to 21 and less than or equal to 59:
Case Is >= 21 And Is <= 59 targetSheet = "Month"
Check if the value is greater than or equal to 60 and less than or equal to 89:
Case Is >= 60 And Is <= 89 targetSheet = "QtrYear"
Check if the value is greater than or equal to 90 and less than or equal to 209:
Case Is >= 90 And Is <= 209 targetSheet = "HalfYear"
Note that each case statement should be on a separate line. I apologize for the confusion again.
- User
The error is not going away. How will you re-write the entire code to be more effective and error free: Private Sub CommandButton1_Click() ' Get data from form Dim desc As String: desc = Me.TaskDescription.Value Dim TaskType As String: TaskType = Me.TaskType.ListIndex + 1 ' Adjust for 0-based indexing Dim Collaborators As String: Collaborators = Me.Collaborators.ListIndex + 1 ' Adjust for 0-based indexing Dim Resources As String: Resources = Me.Resources.ListIndex + 1 ' Adjust for 0-based indexing Dim priority As String: priority = Me.TaskPriority.ListIndex + 1 ' Adjust for 0-based indexing Dim compDate As Date: compDate = Me.CompletionDate.Value Dim stages As String: stages = Me.StagesRequired.ListIndex + 1 ' Adjust for 0-based indexing
- Assistant
Certainly! Here's the corrected and more efficient version of your code:
Private Sub CommandButton1_Click() ' 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 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
- User
In my form, the date value shown is represented as m/dd/yyyy. How can I get the representation to show as dd/mm/yy. I am using the VBA code below to insert the date into the form: Private Sub UserForm_Initialize() ' Run when the form opens Me.DateCreated.Value = Date ' Set to current date directly End Sub
- Assistant
To change the date format in your UserForm, you can use the Format function in VBA. Here's how you can modify your code to display the date in the dd/mm/yy format:
Private Sub UserForm_Initialize() ' Run when the form opens Me.DateCreated.Value = Format(Date, "dd/mm/yy") End Sub
By using the Format function with the "dd/mm/yy" format string, the Date value will be converted to the desired format before being displayed in the DateCreated field on your UserForm.
- User
In my sheet, I enter data into rows from row 2 downwards. I want a vba code that will do the following. When I enter the value 'completed' into column J, then the entire row from column A to K where I entered completed is copied to the next available row in sheet Archive and then the row is deleted.
- Assistant
Certainly! You can use the following VBA code to achieve your requirement:
Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("J:J")) Is Nothing Then ' Check if change occurred in column J Dim cell As Range For Each cell In Intersect(Target, Range("J:J")) ' Loop through changed cells in column J If UCase(cell.Value) = "COMPLETED" Then ' Check if value is "completed" (case-insensitive) Dim lastRow As Long lastRow = Sheets("Archive").Cells(Rows.Count, 1).End(xlUp).Row + 1 ' Find next available row in "Archive" sheet Me.Range("A" & cell.Row & ":K" & cell.Row).Copy Destination:=Sheets("Archive").Range("A" & lastRow) ' Copy row to "Archive" sheet Me.Rows(cell.Row).Delete ' Delete the row in current sheet Exit For ' Exit loop after processing one "completed" entry End If Next cell End If End SubTo use this code, follow these steps:
- User
is it possible to copy the values only without any formatting
- Assistant
Yes, it is possible to copy only the values without any formatting. You can modify the code as shown below:
Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("J:J")) Is Nothing Then ' Check if change occurred in column J Dim cell As Range For Each cell In Intersect(Target, Range("J:J")) ' Loop through changed cells in column J If UCase(cell.Value) = "COMPLETED" Then ' Check if value is "completed" (case-insensitive) Dim lastRow As Long lastRow = Sheets("Archive").Cells(Rows.Count, 1).End(xlUp).Row + 1 ' Find next available row in "Archive" sheet Me.Range("A" & cell.Row & ":K" & cell.Row).Copy Sheets("Archive").Range("A" & lastRow).PasteSpecial xlPasteValues ' Paste values only in "Archive" sheet Application.CutCopyMode = False ' Clear clipboard Me.Rows(cell.Row).Delete ' Delete the row in current sheet Exit For ' Exit loop after processing one "completed" entry End If Next cell End If End Sub - User
I have a sheet named planner that has links to all my other sheet in the workbook. If I delete a row in a sheet the cell address in sheet Planner loses the cell address. how can I prevent this
- Assistant
To prevent the cell references in the "Planner" sheet from losing their connection when a row is deleted in another sheet, you can use the
HYPERLINKfunction along with theINDIRECTfunction in the "Planner" sheet.Assuming that the cell references in the "Planner" sheet are stored in column A, you can modify the formulas in that column to use the
HYPERLINKfunction. Here's an example:=HYPERLINK("#'" & INDIRECT("SheetName") & "'!A1", "Link Text")Replace "SheetName" with the actual sheet name from which you want to create the link. The
INDIRECTfunction retrieves the value in the cell which contains the sheet name. TheHYPERLINKfunction then creates a clickable link with the specified link text.By using this approach, even if a row is deleted in the referenced sheet, the cell references in the "Planner" sheet will still maintain their connection.
Make sure to update the formulas accordingly for each cell reference in column A of the "Planner" sheet.
Note: The above formula assumes that all the referenced sheets have data starting from cell A1. Modify the formula accordingly if the referenced data starts from a different cell address.
- User
The links in my sheet Planner are just simple links like this '=Day!$A2'. I want to maintain this link even if the row in sheet Day is deleted
- Assistant
I apologize for