+ Reply to Thread
Results 1 to 8 of 8

vLookup in a Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    04-17-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    25

    vLookup in a Macro

    So I'm new to excel macros and the forum. Im trying to use a vlookup to enter employees names on a weekly report I download from our home office. I use a workbook I have called vLookup_2012 to do the look up. The problem is whenever we hire someone new I will add it to my vlookup_2012 workbook but the macro dosen't increase the table array for the vlookup. I have tried to use LastRow to see if there was a way to get the vlooup formula to increase based of the last row but I just don't know enough to get that to work. I have spent several hours on this forum looking for something to help but I don't even know what to look for. Please Help.

    Ian

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: vLookup in a Macro

    Could we have a look at your code so far and the structure of the workbook you're using?

  3. #3
    Registered User
    Join Date
    04-17-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: vLookup in a Macro

    Workbooks.Open Filename:= _
            "C:\Users\acthoma\Desktop\Reports\Bulletin\VLookup_2012.xlsx"
        Windows( _
            "Week xx Condensed_Weekly_Advisor_Results_by_Market_Group_or_Region-xx-xx-xxxx.xlsx" _
            ).Activate
        ActiveCell.FormulaR1C1 = _
            "=VLOOKUP(RC[-3],[VLookup_2012.xlsx]Condensed_Weekly_Advisor_Result!C1:C2,2,FALSE)"
        Range("C2").Select
        Selection.End(xlDown).Select
        Range("D216").Select
        Range(Selection, Selection.End(xlUp)).Select
        Range("D2:D216").Select
        Range("D216").Activate
        Selection.FillDown
        Selection.Copy
        Range("C2").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Columns("D:D").Select
        Application.CutCopyMode = False
        Selection.Delete Shift:=xlToLeft
        Range("E2").Select
        ActiveCell.FormulaR1C1 = _
            "=VLOOKUP(RC[-4],[VLookup_2012.xlsx]Condensed_Weekly_Advisor_Result!C1:C4,4,FALSE)"
        Range("D2").Select
        Selection.End(xlDown).Select
        Range("E216").Select
        Range(Selection, Selection.End(xlUp)).Select
        Selection.FillDown
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Columns("D:D").Select
        Application.CutCopyMode = False
        Selection.Delete Shift:=xlToLeft
    Last edited by iwilli; 04-17-2012 at 12:07 PM.

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: vLookup in a Macro

    OK, first off could you read the forum rules and use code tags when you post code, please?

    Secondly, you've got a lot of lines of code in there that aren't doing anything useful. This is one of the drawbacks of using macro recorder.

    Is this code actually in your VLookup_2012 workbook or is it in another book and is there a reason that one formula is looking in the range C1:C2 while the other is using C1:C4? Could they both use the whole of the C column for their lookup?

  5. #5
    Registered User
    Join Date
    04-17-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: vLookup in a Macro

    Sorry about the code. I know now.

    There is a lot of useless code I do know that is from using the recorder. I am still learning the whole macro thing. I know I will get better.

    Lastly the code is on my personal workbook. I need to use it repeatedly as I have to download a new report each week. Now as I look at this my real problem isn't the vlookup but rather this:
        Selection.End(xlDown).Select
        Range("D216").Select
        Range(Selection, Selection.End(xlUp)).Select
        Range("D2:D216").Select
        Range("D216").Activate
        Selection.FillDown
        Selection.Copy
    I want to be able to select the last row select anything that might be below D216. Does this even make sense?

  6. #6
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: vLookup in a Macro

    You want to be able to select everything from D2 to the last row with data in?

    Range(Range("D2"),Range("D60000").End(xlUp)).Select
    Is that it?

  7. #7
    Registered User
    Join Date
    04-17-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: vLookup in a Macro

    This is great. Now for another new guy question. What should your line of code replace? Where do I put it and what needs to come out?

  8. #8
    Registered User
    Join Date
    04-17-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: vLookup in a Macro

    I figured where to put it. Thank you, thank you, thank you! It works perfect.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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