markkeith
New Member
- Joined
- Sep 8, 2020
- Messages
- 17
- Office Version
- 365
- Platform
- Windows
First of all I'm new to VBA and was trying to learn the advantage of using it in Excel.
I have Sheet1 (POS) with blank table that I want to be populated with data from Sheet2 (Records) transcTable when I clicked a button. Source & destination table is NOT same in format & number of columns.
I was trying to loop vlookup (not sure if it's the right way to do) to find Receipt No. from Sheet2 then return all other column data to Sheet1.
Here's the code I made so far.
I have Sheet1 (POS) with blank table that I want to be populated with data from Sheet2 (Records) transcTable when I clicked a button. Source & destination table is NOT same in format & number of columns.
I was trying to loop vlookup (not sure if it's the right way to do) to find Receipt No. from Sheet2 then return all other column data to Sheet1.
Here's the code I made so far.
VBA Code:
Private Sub CommandButton1_Click()
Dim i As Integer
Dim lng As Long
Dim cellx As Range, rowX As Range, numX
Set rowX = Sheet1.Cells(Rows.Count, "A").End(xlUp).Offset(1)
numX = Application.VLookup(Range("receiptNum"), Sheet2.Range("transcTable"), 1, False)
If Not IsError(numX) Then
Range("date").Value = Application.VLookup(Range("receiptNum"), Sheet2.Range("transcTable"), 2, False) 'Date
Range("name").Value = Application.VLookup(Range("receiptNum"), Sheet2.Range("transcTable"), 3, False) 'Name
i = 0
lng = 0
For Each cellx In Sheet1.Range("C6:C34")
i = i + 1
lng = lng + 1
rowX.Offset(lng - 1).Value = i 'Item Num
rowX.Offset(lng - 1, 1).Value = Application.VLookup(Range("receiptNum"), Sheet2.Range("transcTable"), 4, False) 'Type
rowX.Offset(lng - 1, 2).Value = Application.VLookup(Range("receiptNum"), Sheet2.Range("transcTable"), 5, False) 'Description
rowX.Offset(lng - 1, 3).Value = Application.VLookup(Range("receiptNum"), Sheet2.Range("transcTable"), 6, False) 'Qty
rowX.Offset(lng - 1, 4).Value = Application.VLookup(Range("receiptNum"), Sheet2.Range("transcTable"), 7, False) 'Unit
Next cellx
Else
MsgBox "Receipt Number doesn't exist!"
End If
End Sub