I have this code below that I would like to use. Unfortunately after I OK my selection,…
- User
I have this code below that I would like to use. Unfortunately after I OK my selection, it can not make a match between sheet 'OTForm' cell B4 and the selection in sheet 'Overtime'.
Sub CopyMatchingRows() Dim wsOvertime As Worksheet Dim wsOTForm As Worksheet Dim nameToFind As String Dim selectedRange As Range Dim cell As Range Dim matchFound As Boolean Dim copyRow As Range Dim lastRow As Long Dim i As Long
' Set references to the worksheets Set wsOvertime = ThisWorkbook.Sheets("Overtime") Set wsOTForm = ThisWorkbook.Sheets("OTForm") ' Get the name to find from cell B4 in 'OTForm' nameToFind = wsOTForm.Range("B4").Value ' Get the selected range in 'Overtime' On Error Resume Next Set selectedRange = Application.InputBox("Please select a range in 'Overtime'", Type:=8) On Error GoTo 0 ' Check if a range was selected If selectedRange Is Nothing Then MsgBox "No range selected. Please try again.", vbExclamation Exit Sub End If ' Check if the name exists in the selected rangeFor Each cell In selectedRange.Columns(3).Cells 'If Trim(UCase(cell.Value)) = Trim(UCase(nameToFind)) Then If cell.Value = nameToFind Then matchFound = True Exit For End If Next cell
' Copy matching rows to 'OTForm' If matchFound Then lastRow = wsOTForm.Cells(Rows.Count, 1).End(xlUp).Row For Each cell In selectedRange.Columns(3).Cells If cell.Value = nameToFind Then Set copyRow = cell.EntireRow i = i + 1 wsOTForm.Cells(lastRow + i + 10, 1).Value = copyRow.Cells(1, 1).Value wsOTForm.Cells(lastRow + i + 10, 2).Value = copyRow.Cells(1, 4).Value wsOTForm.Cells(lastRow + i + 10, 3).Value = copyRow.Cells(1, 30).Value wsOTForm.Cells(lastRow + i + 10, 4).Value = copyRow.Cells(1, 31).Value wsOTForm.Cells(lastRow + i + 10, 5).Value = copyRow.Cells(1, 32).Value End If Next cell MsgBox "Matching rows copied to 'OTForm'.", vbInformation Else MsgBox "Name of Staff not found in selection. Please check the name entered in 'OTForm'.", vbExclamation End IfEnd Sub
- Assistant
The code you provided seems to be performing the desired task of copying matching rows from the selected range in the "Overtime" sheet to the "OTForm" sheet based on the value in cell B4 of the "OTForm" sheet.
If the code is not working as expected, there could be several reasons for it. Here are a few things you can check:
- Make sure the sheet names ("Overtime" and "OTForm") are correct and exist in your workbook.
- Verify that the value in cell B4 of the "OTForm" sheet matches the desired name to find in the selected range.
- Ensure that the selected range in the "Overtime" sheet contains data in the third column (column C) where the name matching is being performed.
- Check if the values in the selected range and cell B4 of the "OTForm" sheet have the same data type. For example, if the selected range has numeric values, make sure the value in B4 is also numeric, or if the selected range has strings, make sure B4 contains a string.
- If you are manually selecting the range in the "Overtime" sheet, ensure that you are selecting only a single column and that the selection includes the header row. If you select multiple columns or exclude the header row, it may affect the matching and copying process.
- User
All checks are OK except that my selection does not have a header row and will never have a header row.
- Assistant
If your selection does not have a header row, you will need to modify the code to handle the absence of the header row. Here's how you can do it:
-
Remove or comment out the following line that assumes the selected range has a header row:
For Each cell In selectedRange.Columns(3).Cells -
Replace the above line with the following line to loop through all cells in the selected range:
For Each cell In selectedRange.Cells -
Inside the loop, update the references to the cells in the copyRow statement, considering there is no header row. For example:
wsOTForm.Cells(lastRow + i + 10, 1).Value = copyRow.Cells(1, 1).Value wsOTForm.Cells(lastRow + i + 10, 2).Value = copyRow.Cells(1, 4).Value wsOTForm.Cells(lastRow + i + 10, 3).Value = copyRow.Cells(1, 30).Value wsOTForm.Cells(lastRow + i + 10, 4).Value = copyRow.Cells(1, 31).Value wsOTForm.Cells(lastRow + i + 10, 5).Value = copyRow.Cells(1, 32).Value
-
- User
It is no longer giving me the message that the name cannot be found. It completes the code and gives the message “Matching rows copied to ‘OTForm’". But when I go to the OTForm page, no values have been copied over.
- Assistant
If the code is executing without any error message but not copying the values to the "OTForm" sheet, there could be a few reasons for this issue. Here are some possible solutions to troubleshoot the problem:
-
Check if the names of the worksheets ("Overtime" and "OTForm") are spelled correctly, and they exist in your workbook.
-
Verify that the cells in the "Overtime" sheet contain the values you are expecting to match with the name in cell B4 of the "OTForm" sheet. Double-check the data in the selected range to ensure it matches the format and type of the value in cell B4.
-
Ensure that the "OTForm" sheet has enough empty rows below the last row with data to accommodate the copied rows. The code uses the variable to calculate the next available row for copying. Make sure there is enough space below the last row of current data to avoid overwriting any existing information.
-
- User
Everything appears to be OK with both sheets. Can we change the way the values are copied across.
- Assistant
- User
Looking at the code, I beleive it does not identify the correct address of where the values should be pasted. If matches are found in the selection made in the Overtime sheet, then the row values of each match should be pasted in the OTForm sheet starting at row 11.
Sub CopyMatchingRows() Dim wsOvertime As Worksheet Dim wsOTForm As Worksheet Dim nameToFind As String Dim selectedRange As Range Dim cell As Range Dim matchFound As Boolean Dim copyRow As Range Dim lastRow As Long Dim i As Long
- Assistant
Apologies for the oversight. You're correct that the code I provided does not identify the correct address for pasting the values in the "OTForm" sheet.