+ Reply to Thread
Results 1 to 10 of 10

Using Vlookup in VBA code

  1. #1
    Registered User
    Join Date
    03-14-2020
    Location
    CT, USA
    MS-Off Ver
    2013
    Posts
    3

    Using Vlookup in VBA code

    VBA.png
    Dear Sir;
    I need a VBA code that copy some cells based on a specific value.
    the details shown in attached image
    tnx

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

    Re: Using Vlookup in VBA code

    Please Login or Register  to view this content.
    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
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Another way …


    A bit unclear as the copy destination is missing …

    As a VBA beginner starter (not tested as the attachment is missing too ! ) :

    PHP Code: 
    Sub Demo1()
        
    Dim FT
        With ActiveSheet
    .UsedRange.Columns
            F 
    Application.Match(.Cells(15).Value2, .Item(1), 0)
            
    Application.Match(.Cells(25).Value2, .Item(1), 0)
            If 
    IsError(F) Or IsError(TThen Beep Else Range(.Cells(F2), .Cells(T2)).Copy .Cells(17)
        
    End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,882

    Re: Using Vlookup in VBA code

    Here is Chip Pearson's tutorial on the subject: http://www.cpearson.com/excel/Callin...ionsInVBA.aspx
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  5. #5
    Registered User
    Join Date
    03-14-2020
    Location
    CT, USA
    MS-Off Ver
    2013
    Posts
    3

    Re: Using Vlookup in VBA code

    Thank you for reply mehmetcik;
    your code correctly identifies E1 and E2 from column A and paste to column C. But what i need is just copy the corresponding values from column B (B5:B22 for this example). Please the attached photo for more detail.
    tnx
    Attachment 667389
    Last edited by Anis574; 03-15-2020 at 02:56 AM.

  6. #6
    Registered User
    Join Date
    03-14-2020
    Location
    CT, USA
    MS-Off Ver
    2013
    Posts
    3

    Re: Using Vlookup in VBA code

    Hi Marc L;
    Nice job, it works.
    Is that possible to assign the worksheet in this code. I mean if i be in another worksheet (sheet 4 for example) the automatically executes in sheet1?
    and is that possible that i enter the values of F and T dynamically (by an inputbox)?
    thanks
    Last edited by Anis574; 03-15-2020 at 07:12 AM.

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Using Vlookup in VBA code


    You can replace ActiveSheet …

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Using Vlookup in VBA code

    Hey Marc L,

    In your code above you have a Dim statement of:
    Dim F, T
    I use Option Explicit as the first line of my code to insure each variable is typed (as in string, long, double,...) Did you know your two variables above start as type Variant? Also if you use things like:
    Dim i, j, k as integer , only the k will be an integer and the i and j will be variant. Just an FYI. Read:
    http://www.learnexcelmacro.com/wp/20...dim-statement/
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Using Vlookup in VBA code


    Hi Marvin !

    You can check yourself both variables must be Variant as you can read in Excel help for MATCH worksheet function !

    I also explained several times why within this forum like in others, the reason why I wrote the procedure as it is …
    Last edited by Marc L; 03-15-2020 at 08:51 PM.

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

+ 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] Please tell me simple search vba code ? my vba vlookup code not working..
    By aliakbaram85 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-27-2018, 01:54 AM
  2. VLookup Zip Code Help
    By BoB_CA in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 03-07-2017, 03:59 PM
  3. Vlookup code to Index/Match code help
    By dubcap01 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-24-2015, 07:05 PM
  4. Replies: 1
    Last Post: 11-13-2013, 10:28 AM
  5. [SOLVED] VBA code for change vlookup formula to vlookup formula with ISNA
    By erprasannaa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-12-2013, 03:33 AM
  6. VlookUp-VBA CODE
    By CESAR V. ARROYO in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-11-2011, 06:07 AM
  7. Optimizing the Vlookup code in VB. Repitive code with different referencing range.
    By raknahs in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-23-2010, 05:03 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