VB Script for Vlookup

bmontoni

New Member
Joined
Mar 1, 2022
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am wondering if it is possible to use VB Script to perform the following v-lookup:

I have a workbook with 2 tabs. I want to add a new column to the right in the table below that uses the "Document" field to lookup the "Mgs." field in the second table. Is this possible? I would need it to go to the last row as the number of rows could change.

1657913192723.png


1657913229149.png




Thanks in advance!
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi,

Check below code:

VBA Code:
Sub insertMsg()
    On Error Resume Next
    Dim lastRow As Integer, rowno As Integer
    Dim lookupRange As Range
    
    With Sheets("Delivery-with ZIV")
        'Insert new column
        .Columns("I:I").Insert Shift:=xlToRight
        
        'Heading of new column
        .Range("I1") = "Msg"
        
        'Identifying the total rows
        lastRow = .Cells(Rows.Count, "H").End(xlUp).Row
        
        'Setting lookup range to check msg value
        Set lookupRange = Sheets("VBFS - DeliveryWoZIV").Range("B1:G" & Sheets("VBFS - DeliveryWoZIV").Cells(Rows.Count, "G").End(xlUp).Row)
        
        'Loop to get values using VLOOKUP function
        For rowno = 2 To lastRow
            .Range("I" & rowno) = Application.WorksheetFunction.VLookup(.Range("H" & rowno), lookupRange, 6, False)
        Next
    End With
End Sub
 
Upvote 0
Try this:

VBA Code:
Sub fxVloolup()
  With Sheets("Delivery-with ZIV").Range("AA2:AA" & Sheets("Delivery-with ZIV").Range("H" & Rows.Count).End(3).Row)
    .Formula = "=IFERROR(VLOOKUP(H2,'VBFS - DeliveryWoZIV'!B:G,6,0),""Not Found"")"
    .Value = .Value
  End With
End Sub

NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

Example:
Dante Amor
ACHAA
1Sorgdocument
2us0238250931064
3us02382509388Not Found
Delivery-with ZIV


Dante Amor
ABG
1documentmsg
238250931064
338250931164
VBFS - DeliveryWoZIV
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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