I have a VBA code that searches for one value in all my worksheets and pastes it into a…
- User
I have a VBA code that searches for one value in all my worksheets and pastes it into a single column. I need to adjust the code so that for each value equal to 'Reminder' found in column B of all the worksheets, I want to then copy Column C, Column J, Column L, of the same row as column B to sheet 'Reminders' and paste Column J into Columnn D in the sheet Reminders, column L into column E of the sheet Reminders and column C into column F of the sheet Reminders, starting from row 8. Can you please help?
Public Sub FindAllReminder()
Dim xWs As Worksheet Dim xCWs As Worksheet Dim Xrg As Range Dim xStrName As String Dim xRStr As String Dim xRRg As Range Dim xC As Integer Dim wsName As String
On Error Resume Next
Worksheets("Reminder").Range("D8:F20").ClearContents
Application.DisplayAlerts = False
xStr = "Reminders" xRStr = "Reminder"
Set xCWs = ActiveWorkbook.Worksheets.Item(xStr) If xCWs Is Nothing Then Set xCWs = ActiveWorkbook.Worksheets.Add xCWs.Name = xStr End If
xC = 1 For Each xWs In ActiveWorkbook.Worksheets wsName = xWs.Name If Len(wsName) = 3 Then Set Xrg = xWs.Range("B:B") Set Xrg = Intersect(Xrg, xWs.UsedRange)
For Each xRRg In Xrg If xRRg.Value = xRStr Then xWs.Range(xRRg.Offset(0, -1), xRRg.Offset(0, 9)).Copy xCWs.Cells(xC, 1).PasteSpecial xlPasteValuesAndNumberFormats xC = xC + 1 End If
Next xRRg End If Next xWs Application.CutCopyMode = False Application.DisplayAlerts = True Application.CutCopyMode = True End Sub
- Assistant
Here's the adjusted code:
Public Sub FindAllReminder()
Dim xWs As Worksheet Dim xCWs As Worksheet Dim Xrg As Range Dim xStrName As String Dim xRStr As String Dim xRRg As Range Dim wsName As String Dim i As Long
On Error Resume Next
Application.DisplayAlerts = False
' Define sheet and range to paste reminders Set xCWs = ActiveWorkbook.Worksheets("Reminders") xCWs.Range("D8:F20").ClearContents
' Find "Reminder" in column B of each worksheet For Each xWs In ActiveWorkbook.Worksheets wsName = xWs.Name If Len(wsName) = 3 Then ' Only look at worksheets with 3-letter names Set Xrg = xWs.Range("B:B") Set Xrg = Intersect(Xrg, xWs.UsedRange) For Each xRRg In Xrg If xRRg.Value = "Reminder" Then ' Found a reminder, copy columns C, J, and L to Reminders sheet i = xCWs.Cells(Rows.Count, 1).End(xlUp).Row + 1 ' Find next empty row in Reminders sheet xWs.Range(xRRg.Offset(0, 1), xRRg.Offset(0, 1).End(xlToRight)).Copy Destination:=xCWs.Range("C" & i) xWs.Range(xRRg.Offset(0, 9)).Copy Destination:=xCWs.Range("D" & i) xWs.Range(xRRg.Offset(0, 11)).Copy Destination:=xCWs.Range("E" & i) End If Next xRRg End If Next xWs
- User
The code found some information but it placed it in the wrong place. Results copied from column C should be pasted in column F of the sheet Reminders I do not need to copy column D Results copied from column J should be pasted into column D of the sheet Reminders and results copied from column L should be pasted in column E of the sheet Reminders. Also, the available range in sheet Reminder where the values can be pasted is D8:F20, I have values in the rows above and beneath this range. D8:F20 is my only blank space.
- Assistant
Here's the updated code:
Public Sub FindAllReminder()
Dim xWs As Worksheet Dim xCWs As Worksheet Dim Xrg As Range Dim xRStr As String Dim xRRg As Range Dim wsName As String Dim i As Long
On Error Resume Next
Application.DisplayAlerts = False
' Define sheet and range to paste reminders Set xCWs = ActiveWorkbook.Worksheets("Reminders") xCWs.Range("D8:F20").ClearContents
' Find "Reminder" in column B of each worksheet For Each xWs In ActiveWorkbook.Worksheets wsName = xWs.Name If Len(wsName) = 3 Then ' Only look at worksheets with 3-letter names Set Xrg = xWs.Range("B:B") Set Xrg = Intersect(Xrg, xWs.UsedRange) For Each xRRg In Xrg If xRRg.Value = "Reminder" Then ' Found a reminder, copy columns C, J, and L to Reminders sheet i = xCWs.Cells(Rows.Count, 4).End(xlUp).Row + 1 ' Find next empty row in Reminders sheet xCWs.Range("F" & i).Value = xRRg.Offset(0, -2).Value ' Copy from column C to column F xCWs.Range("D" & i).Value = xRRg.Offset(0, 8).Value ' Copy from column J to column D xCWs.Range("E" & i).Value = xRRg.Offset(0, 10).Value ' Copy from column L to column E End If Next xRRg End If Next xWs
- User
It is still not pasting the data within the desired range D8:F20 and it is not copying Column C which needs to be pasted into column F of the sheet Reminders
- Assistant
I apologize for the oversight. Here's an updated code that should copy Column C to Column F of the "Reminders" sheet, and paste all values within the desired range D8:F20:
Public Sub FindAllReminder()
Dim xWs As Worksheet Dim xCWs As Worksheet Dim Xrg As Range Dim xRStr As String Dim xRRg As Range Dim wsName As String Dim i As Long
On Error Resume Next
Application.DisplayAlerts = False
' Define sheet and range to paste reminders Set xCWs = ActiveWorkbook.Worksheets("Reminders") With xCWs .Range("D8:F20").ClearContents .Range("D7:F7").Value = Array("Column J", "Column L", "Column C") 'Add header End With
- User
It pasted the Headers but not the data
- Assistant
I apologize for the issues you're encountering. Could you try the following code which pastes the data first in a temp worksheet, then checks for the number of rows and paste them in the desired range in the "Reminders" sheet?
- User
it is still not getting nor pasting the data found
- Assistant
I apologize for the continued issues with the code. Here's another attempt, where I've made some changes to simplify the code and ensure that the data is properly copied and pasted. Please give this code a try:
- User
The code is not finding the Reminders that do exist. The Pop Up is telling me No Reminders Found
- Assistant
I apologize for the continued issues. In this case, it may be that the worksheet name check is not being done correctly. If your worksheets are not in the format of three-letter names, the code may be skipping over them. Here's an updated version of the code that should check all worksheets regardless of their name format:
Public Sub FindAllReminder()
Dim xWs As Worksheet Dim xCWs As Worksheet Dim Xrg As Range Dim xRRg As Range Dim i As Long Dim foundReminder As Boolean
On Error Resume Next
Application.DisplayAlerts = False
' Define sheet and range to paste reminders Set xCWs = ActiveWorkbook.Worksheets("Reminders") With xCWs .Range("D8:F20").ClearContents .Range("D7:F7").Value = Array("Column J", "Column L", "Column C") 'Add header End With
' Find "Reminder" in column B of each worksheet For Each xWs In ActiveWorkbook.Worksheets Set Xrg = xWs.Range("B:B") Set Xrg = Intersect(Xrg, xWs.UsedRange) For Each xRRg In Xrg If xRRg.Value = "Reminder" Then ' Found a reminder, copy columns C, J, and L to Reminders sheet foundReminder = True i = xCWs.Cells(xCWs.Rows.Count, 4).End(xlUp).Row + 1 ' Find next empty row in Reminders sheet xCWs.Range("F" & i).Value = xRRg.Offset(0, -2).Value ' Copy from column C to column F xCWs.Range("D" & i).Value = xRRg.Offset(0, 8).Value ' Copy from