copy paste by vba

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You haven't specified any worksheet in your two workbooks (i didn't look at your files) so I assume they only have one workhseet so try this:
Code:
Sub test()
Workbooks("price.xlsx").Activate
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastrow, 7))
Workbooks("leverage.xlsx").Activate
lastrow2 = Cells(Rows.Count, "A").End(xlUp).Row
inarr2 = Range(Cells(1, 1), Cells(lastrow2, 1))
  
  For i = 1 To lastrow2
   For j = 1 To lastrow
    If inarr2(i, 1) = inarr(j, 1) Then
     Cells(i, 6) = inarr(j, 7)
     Exit For
    End If
   Next j
  Next i


End Sub
 
Last edited:
Upvote 0
Another option
Code:
Sub kajal12()
   Dim Ary As Variant
   Dim i As Long
   Dim Dic As Object
   Dim Cl As Range
   
   Set Dic = CreateObject("Scripting.dictionary")
   With Workbooks("Price.xlsx").Sheets("[COLOR=#ff0000]sheet1[/COLOR]")
      Ary = .Range("A2", .Range("A" & Rows.Count).End(xlUp).Offset(, 6)).Value2
   End With
   For i = 1 To UBound(Ary)
      Dic(Ary(i, 1)) = Ary(i, 7)
   Next i
   With Workbooks("leverage.xlsx").Sheets("[COLOR=#ff0000]sheets1[/COLOR]")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Cl.Offset(, 5).Value = Dic(Cl.Value)
      Next Cl
   End With
End Sub
Change sheet names in red to suit.
 
Upvote 0
Thnx Sir for giving ur precious time and great support to this post sir
Thnx Alot Have a Great Day
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
Change file paths to suit
Code:
Sub kajal12()
   Dim Ary As Variant
   Dim i As Long
   Dim Dic As Object
   Dim Cl As Range
   Dim Wbk1 As Workbook, Wbk2 As Workbook
   
   Set Wbk1 = Workbooks.Open("[COLOR=#ff0000]C:\Mrexcel\[/COLOR]Price.xlsx")
   Set Wbk2 = Workbooks.Open("[COLOR=#ff0000]C:\Mrexcel\[/COLOR]leverage.xlsx")
   Set Dic = CreateObject("Scripting.dictionary")
   With Wbk1.Sheets("sheet1")
      Ary = .Range("A2", .Range("A" & Rows.Count).End(xlUp).Offset(, 6)).Value2
   End With
   For i = 1 To UBound(Ary)
      Dic(Ary(i, 1)) = Ary(i, 7)
   Next i
   With Wbk2.Sheets("sheets1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Cl.Offset(, 5).Value = Dic(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,517
Messages
6,114,089
Members
448,548
Latest member
harryls

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