VBA based on cell value search for it in the table and then copy a row (help with existing code)

DariusUK

New Member
Joined
Nov 6, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I managed to have to make it partially work; however, it copied and pasted a whole range from Data sheet - I want only the row that matches C6 from Entry sheet .

Here is the code:

Sub Find_Order()
Dim entrySheet As Worksheet
Dim dataSheet As Worksheet
Dim checkSheet As Worksheet
Dim searchValue As Variant
Dim lastDataRow As Long

' Define the worksheets
Set entrySheet = ThisWorkbook.Sheets("Entry")
Set dataSheet = ThisWorkbook.Sheets("Data")
Set checkSheet = ThisWorkbook.Sheets("Check")

' Get the search value from Entry C6
searchValue = entrySheet.Range("C6").Value

' Find the last row in the Data sheet's column A
lastDataRow = dataSheet.Cells(dataSheet.Rows.Count, "A").End(xlUp).Row

' Check if the search value is found in Data sheet's column A
If Not IsError(Application.Match(searchValue, dataSheet.Range("A1:A" & lastDataRow), 0)) Then
' If found, copy the entire column to the Check sheet
dataSheet.Range("A" & lastDataRow).EntireRow.Copy
checkSheet.Rows(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
Else
MsgBox "Value not found in Data sheet.", vbInformation
End If

Application.CutCopyMode = False ' Clear the clipboard
End Sub

Any help will be appreciated
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi @DariusUK and Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

Replace your code with the following:

VBA Code:
Sub Find_Order()
  Dim entrySheet As Worksheet
  Dim dataSheet As Worksheet
  Dim checkSheet As Worksheet
  Dim searchValue As Variant
  Dim f As Range
 
  ' Define the worksheets
  Set entrySheet = ThisWorkbook.Sheets("Entry")
  Set dataSheet = ThisWorkbook.Sheets("Data")
  Set checkSheet = ThisWorkbook.Sheets("Check")
 
  searchValue = entrySheet.Range("C6").Value        ' Get the search value from Entry C6
  ' Check if the search value is found in Data sheet's column A
  Set f = dataSheet.Range("A:A").Find(searchValue, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    ' If found, copy the entire row to the Check sheet
    f.EntireRow.Copy
    checkSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
  Else
    MsgBox "Value not found in Data sheet.", vbInformation
  End If
 
  Application.CutCopyMode = False ' Clear the clipboard
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,112
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top