Phaedrus1301
New Member
- Joined
- Feb 21, 2024
- Messages
- 2
- Office Version
- 2010
- Platform
- Windows
Hello,
I am fairly new to VBA and I was given a task regarding it, I am using MS Excel 2010. Here is my attempt to explain my end goal and code I've written till now to reach the same.
Workbook1 has Sheet1,
Col A has `uniqueProductID` from company A
Col B has `Price`
Col D has `uniqueProductID` from company B
Col E has `Price`
I was trying to write a vba script, which checks for matching unique ID's in both col A & col B and then once the id is matching it should put data in sheet2
Col A - `UniqueMatchedID`
Col B - `Price from company A`
Col C - `Price from company B`
I wrote small vb script for this (code below this), which is running without any errors however it is not giving any output, can someone please guide me with this?
PS - I have started both loops from free as 1st row contains of merged block of company name and then 2 contains title, data starts from the 3rd. I am also attaching images for sheet1 and sheet2 expected data as that might help.
Thanks in advanced for all your help.
I am fairly new to VBA and I was given a task regarding it, I am using MS Excel 2010. Here is my attempt to explain my end goal and code I've written till now to reach the same.
Workbook1 has Sheet1,
Col A has `uniqueProductID` from company A
Col B has `Price`
Col D has `uniqueProductID` from company B
Col E has `Price`
I was trying to write a vba script, which checks for matching unique ID's in both col A & col B and then once the id is matching it should put data in sheet2
Col A - `UniqueMatchedID`
Col B - `Price from company A`
Col C - `Price from company B`
I wrote small vb script for this (code below this), which is running without any errors however it is not giving any output, can someone please guide me with this?
VBA Code:
Sub CompareAndCopy()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lastRow1 As Long
Dim lastRow2 As Long
Dim i As Long
Dim j As Long
Dim rowNum As Long
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
rowNum = 2
For i = 3 To lastRow1
For j = 3 To lastRow1
If ws1.Cells(i, "A").Value = ws1.Cells(j, "D").Value Then
ws2.Cells(rowNum, "A").Value = ws1.Cells(i, "A").Value
ws2.Cells(rowNum, "B").Value = ws1.Cells(i, "B").Value
ws2.Cells(rowNum, "C").Value = ws1.Cells(j, "E").Value
rowNum = rowNum + 1
End If
Next j
Next i
MsgBox "Comparison and copying completed!", vbInformation
End Sub
PS - I have started both loops from free as 1st row contains of merged block of company name and then 2 contains title, data starts from the 3rd. I am also attaching images for sheet1 and sheet2 expected data as that might help.
Thanks in advanced for all your help.
Attachments
Last edited by a moderator: