VBA code to find value in once sheet and paste it in another sheet

Tanyaann1995

Board Regular
Joined
Mar 24, 2021
Messages
62
Office Version
  1. 2016
Platform
  1. Windows
Hi,

In sheet 2, when I write a part number in column E24, I want the code to find that part in Sheet 3. Please see attached pictures of how both the sheets look like.
The below is my code but an error message "Application or object defined error" keeps popping up after the first For loop. Pls check and advise.

VBA Code:
Sub Price()

Dim pno As Double
Dim LastRow As Long
Dim i As Integer
Dim LastRowinMainSheet As Long
Dim j As Integer

LastRowinMainSheet = Cells.Find(What:="*", _
                    After:=Range("E23"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row

LastRow = Worksheets(3).UsedRange.SpecialCells(x1CellTypeLastCell).Row

For j = 24 To LastRowinMainSheet
Worksheets(2).Cells(j, 5).Value = pno

For i = 3 To LastRow

If Worksheets(3).Cells(i, 2).Value = pno Then
Worksheets(3).Cells(i, 3).Copy
Worksheets(2).Cells(j, 4).PasteSpecial xlPasteValues
Worksheets(3).Cells(i, 4).Copy
Worksheets(2).Cells(j, 6).PasteSpecial xlPasteValues
Worksheets(3).Cells(i, 5).Copy
Worksheets(2).Cells(j, 7).PasteSpecial xlPasteValues
Worksheets(3).Cells(i, 14).Copy
Worksheets(2).Cells(j, 12).PasteSpecial xlPasteValues
End If

Next i

Next j


End Sub
 

Attachments

  • Capture9.PNG
    Capture9.PNG
    27.5 KB · Views: 9
  • Capture10.PNG
    Capture10.PNG
    53.1 KB · Views: 9
Last edited by a moderator:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Ok, try
VBA Code:
pno = ThisWorkbook.Worksheets(2).Cells(j, 5).Value
 
Upvote 0
Ok, try
VBA Code:
pno = ThisWorkbook.Worksheets(2).Cells(j, 5).Value
Thanks for the above code. Currently, this code is taking too long since it checks if the value is equal to every other value in the other workbook. Please advise if there a method to just find that value instead of using If statement to check if that value is equal to every other value. The below is the current code.

For j = 24 To LastRowinMainSheet
pno = ThisWorkbook.Worksheets(2).Cells(j, 5).Value

For i = 3 To LastRow

If f.Worksheets(1).Cells(i, 2).Value = pno Then
ThisWorkbook.Worksheets(2).Cells(j, 4).Value = f.Worksheets(1).Cells(i, 3).Value
ThisWorkbook.Worksheets(2).Cells(j, 6).Value = f.Worksheets(1).Cells(i, 4).Value
ThisWorkbook.Worksheets(2).Cells(j, 7).Value = f.Worksheets(1).Cells(i, 5).Value
ThisWorkbook.Worksheets(2).Cells(j, 12).Value = f.Worksheets(1).Cells(i, 14).Value

End If

Next i

Next j

After finding the value, I will still need to copy the corresponding part numbers, description and price and paste them in the other workbook similar to above code.
 
Upvote 0
You can use Range.Find to find the value rather than looping. But as that's a totally different question, you will need to start a new thread if you need help with it.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,850
Members
449,051
Latest member
excelquestion515

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