I want a VBA code that will do the following. When I select a value from a list in A1,…
- User
I want a VBA code that will do the following. When I select a value from a list in A1, In all sheets in my workbook that have a name with only three letters, find the matching value in column B, then for each match, copy the row B:J to sheet 'CTasks' B:J starting from row two. For each match found, I also want the sheet name where the match is found to be written to column K in sheet CTasks.
- Assistant
Sure! Here's a VBA code that will perform the actions you described:
Sub FindAndCopyRows() Dim selectedValue As String Dim ws As Worksheet Dim cTasksWs As Worksheet Dim lastRow As Long, pasteRow As Long ' Get the selected value from cell A1 selectedValue = Range("A1").Value ' Set the "CTasks" worksheet as the destination for copied rows Set cTasksWs = ThisWorkbook.Worksheets("CTasks") ' Clear the previous data in CTasks worksheet cTasksWs.Range("B2:J" & cTasksWs.Cells(Rows.Count, "B").End(xlUp).Row).ClearContents cTasksWs.Range("K2:K" & cTasksWs.Cells(Rows.Count, "K").End(xlUp).Row).ClearContents ' Loop through all sheets in the workbook For Each ws In ThisWorkbook.Worksheets ' Check if the sheet name has only three letters If Len(ws.Name) = 3 Then ' Find the matching value in column B lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row For i = 2 To lastRow If ws.Cells(i, "B").Value = selectedValue Then ' Copy the row B:J to CTasks sheet pasteRow = cTasksWs.Cells(cTasksWs.Rows.Count, "B").End(xlUp).Row + 1 ws.Range("B" & i & ":J" & i).Copy Destination:=cTasksWs.Range("B" & pasteRow & ":J" & pasteRow) ' Write the sheet name to column K in CTasks sheet cTasksWs.Cells(pasteRow, "K").Value = ws.Name End If Next i End If Next ws End Sub - User
In the part where it clears previous data, can I also have a line that clears comments
- Assistant
Yes, you can modify the code to clear comments as well. Here's the updated code that clears both data and comments in the CTasks worksheet:
Sub FindAndCopyRows() Dim selectedValue As String Dim ws As Worksheet Dim cTasksWs As Worksheet Dim lastRow As Long, pasteRow As Long ' Get the selected value from cell A1 selectedValue = Range("A1").Value ' Set the "CTasks" worksheet as the destination for copied rows Set cTasksWs = ThisWorkbook.Worksheets("CTasks") ' Clear the previous data and comments in CTasks worksheet cTasksWs.Range("B2:J" & cTasksWs.Cells(Rows.Count, "B").End(xlUp).Row).Clear cTasksWs.Range("K2:K" & cTasksWs.Cells(Rows.Count, "K").End(xlUp).Row).Clear ' Loop through all sheets in the workbook For Each ws In ThisWorkbook.Worksheets ' Check if the sheet name has only three letters If Len(ws.Name) = 3 Then ' Find the matching value in column B lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row For i = 2 To lastRow If ws.Cells(i, "B").Value = selectedValue Then ' Copy the row B:J to CTasks sheet pasteRow = cTasksWs.Cells(cTasksWs.Rows.Count, "B").End(xlUp).Row + 1 ws.Range("B" & i & ":J" & i).Copy Destination:=cTasksWs.Range("B" & pasteRow & ":J" & pasteRow) ' Write the sheet name to column K in CTasks sheet cTasksWs.Cells(pasteRow, "K").Value = ws.Name End If Next i End If Next ws End Sub - User
Can you please also write a code so that when I double click on a cell in K2:K101, it will activate the sheet with the same name as the value of the cell in K
- Assistant
Certainly! Here's the code that will activate the sheet with the same name as the value in K2:K101 when double-clicked:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Dim ws As Worksheet Dim sheetName As String ' Check if the double-click occurred in column K If Not Intersect(Target, Range("K2:K101")) Is Nothing Then Application.EnableEvents = False sheetName = Target.Value ' Loop through all worksheets in the workbook For Each ws In ThisWorkbook.Worksheets ' Check if the worksheet name matches the value in column K If ws.Name = sheetName Then ws.Activate Exit For End If Next ws Application.EnableEvents = True End If End SubTo use the code, follow these steps:
- Press ALT + F11 to open the VBA editor in Excel.