Hello everyone,
I'm having difficulties with understanding how VLOOKUP function works.
To simplify - I have one workbook with two sheets Sheet1 (Columns A (Stocks Sybol), B (Number of Stocks), C (Price)) and Sheet2 (Columns A (Stocks Symbol), D (Number of Stocks), E (Currency)) .
The aim is to find Stock symbol from Column A Sheet2 in the Column A of Sheet1 and corresponding "Number of Stocks" and "Price" values in Columns B and C of Sheet1 should be pasted into Columns D and E (correspondingly) of Sheet2.
I found on internet vba code (as it follows below) to transfer single value from single column at a time but couldn't make it work either.
Please find image files of the sheets below as an attachment.
Sub Shares_Info()
Dim Sheet1Ws As Worksheet
Dim Sheet2Ws As Worksheet
Dim Sheet1LastRow As Long
Dim Sheet2LasRow As Long
Dim x As Long
Dim dataRng As Range
Set Sheet1Ws = ThisWorkbook.Worksheets("Sheet1")
Set Sheet2Ws = ThisWorkbook.Worksheets("Sheet2")
Sheet1LastRow = Sheet1Ws.Range("A" & Rows.Count).End(xlUp)
Sheet2LastRow = Sheet2Ws.Range("A" & Rows.Count).End(xlUp)
Set dataRng = Sheet2Ws.Range("D:E" & Sheet2LastRow)
For x = 2 To Sheet1LastRow
On Error Resume Next
Sheet1Ws.Range("D" & x).Value = Application.WorksheetFunction.VLookup( _
Sheet2Ws.Range("A" & x).Value, dataRng, 4, False)
Next x
End Sub
I'm having difficulties with understanding how VLOOKUP function works.
To simplify - I have one workbook with two sheets Sheet1 (Columns A (Stocks Sybol), B (Number of Stocks), C (Price)) and Sheet2 (Columns A (Stocks Symbol), D (Number of Stocks), E (Currency)) .
The aim is to find Stock symbol from Column A Sheet2 in the Column A of Sheet1 and corresponding "Number of Stocks" and "Price" values in Columns B and C of Sheet1 should be pasted into Columns D and E (correspondingly) of Sheet2.
I found on internet vba code (as it follows below) to transfer single value from single column at a time but couldn't make it work either.
Please find image files of the sheets below as an attachment.
Sub Shares_Info()
Dim Sheet1Ws As Worksheet
Dim Sheet2Ws As Worksheet
Dim Sheet1LastRow As Long
Dim Sheet2LasRow As Long
Dim x As Long
Dim dataRng As Range
Set Sheet1Ws = ThisWorkbook.Worksheets("Sheet1")
Set Sheet2Ws = ThisWorkbook.Worksheets("Sheet2")
Sheet1LastRow = Sheet1Ws.Range("A" & Rows.Count).End(xlUp)
Sheet2LastRow = Sheet2Ws.Range("A" & Rows.Count).End(xlUp)
Set dataRng = Sheet2Ws.Range("D:E" & Sheet2LastRow)
For x = 2 To Sheet1LastRow
On Error Resume Next
Sheet1Ws.Range("D" & x).Value = Application.WorksheetFunction.VLookup( _
Sheet2Ws.Range("A" & x).Value, dataRng, 4, False)
Next x
End Sub