+ Reply to Thread
Results 1 to 3 of 3

Macro to execute a vlookup from A1 to last cell used

  1. #1
    Registered User
    Join Date
    09-28-2017
    Location
    London
    MS-Off Ver
    Mac 2016
    Posts
    1

    Macro to execute a vlookup from A1 to last cell used

    Hi Guys,

    I am here to ask help on creating a macro to make my life easier.

    I have 2 sheets, Database and Input. In Database I have 2 columns: "Users" and "Country", I need to have the macro to autofill the column A in the Input sheet when I write the User in the column B.

    The Database is updated daily so I can remove or add entries, this is why I need to have a command that keeps executing the vlookup in the column A UNTIL the end cell of column B.

    edited:

    this seems to work if anyone is looking for the same:

    Sub Hello()

    Dim ws As Worksheet
    Dim LastRow As Long
    Dim TargetRange As Range

    On Error GoTo MyErrorHandler:

    Set ws = Sheets("Database")

    LastRow = ws.Cells(Rows.Count, "B").End(xlUp).Row

    Set xWs = Sheets("Input")

    LastRowXX = xWs.Cells(Rows.Count, "B").End(xlUp).Row

    Set TargetRange = ws.Range("A1:B" & LastRowXX)

    result = Application.WorksheetFunction.VLookup(Sheets("Input").Range("B1:B" & LastRow), TargetRange, 2, False)

    'MsgBox result

    Range("A1:A" & LastRowXX) = result

    MyErrorHandler:
    If Err.Number = 1004 Then
    MsgBox "error"
    End If

    End Sub
    Last edited by pinky2; 09-28-2017 at 06:27 PM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Macro to execute a vlookup from A1 to last cell used

    Hello and welcome.

    Unfortunately you have broken one of the forum rules and so no-one can help you at this time.

    You need to insert code tags around your code.

    The following standard message explains what you need to do.


    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Macro to execute a vlookup from A1 to last cell used

    While you are at it.

    Please post a copy of your spreadsheet.

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Execute macro based on cell value
    By Maike in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-25-2017, 08:38 AM
  2. [SOLVED] How to execute a macro when a cell value changes?
    By sylvainsyl20 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-20-2016, 09:22 AM
  3. [SOLVED] Execute macro on cell value change
    By tom.mat.walker in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-15-2015, 05:14 AM
  4. Execute a macro in a closed workbook, or open and execute without the user noticing
    By cearaujo123 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-09-2015, 06:44 PM
  5. Execute Cell with a Macro (timer)
    By flds in forum Excel General
    Replies: 6
    Last Post: 05-13-2010, 07:26 AM
  6. macro to execute code from cell.value?
    By wamp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-19-2010, 04:29 AM
  7. On Cell change, execute macro
    By des_wes in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-19-2007, 07:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1