Closed Thread
Results 1 to 22 of 22

VLOOKUP with macro

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    475

    VLOOKUP with macro

    Hello everybody.
    In the attached workbook there is a formula cerca.vert

    =SE(L8="";"";SE(VAL.NON.DISP(CERCA.VERT(L8;data_base!$A$2:$B$1500;2;0));"v.n.d.";CERCA.VERT(L8;data_base!$A$2:$B$1500;2;0)))
    =IF(L8="","",IF(ISNA(VLOOKUP(L8,data_base!$A$2:$B$1500,2,0)),"v.n.d.",VLOOKUP(L8,data_base!$A$2:$B$1500,2,0)))
    is it possible to turn it into a macro at each new insertion in the range L7:L31 ?
    Thank you.
    xam
    Attached Files Attached Files

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,453

    Re: VLOOKUP with macro

    If you mean extending the formula when you insert a row, no macro needed
    Make your range an Excel Table, and any added row will create the formula automatically

  3. #3
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    475

    Re: VLOOKUP with macro

    Hi peppe le mokko
    I don't mean what you say.
    I ask to translate the formula into a macro at each insertion

  4. #4
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    475

    Re: VLOOKUP with macro

    Hi,
    i made this macro but it doesn't work
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    'Sub vlookup_alternative()
    
        Dim data As Long
        Dim Rng As Range
        
        data = Worksheets("S1").Range("L7:L31").Value 'dove scrivo
        
            With Sheets("data_base").Range("A2:A") 'data base dove nome
            
                Set Rng = .Find(What:=data, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                                
                If Not Rng Is Nothing Then
                
            Sheets("data_base").Cells(Rng.Row, 2).Value = Worksheets("S1").Range("B7:B31").Value '5 colonna nome foglio        
                    
                    
                End If
                
            End With
            
    End Sub
    it must work in the range L7:L31 and return the value in B7:B31.
    xam
    Attached Files Attached Files

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,596

    Re: VLOOKUP with macro

    Do you mean like this?
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim r As Range
        If Intersect(Target, [L7:L31]) Is Nothing Then Exit Sub
        Application.EnableEvents = False
        For Each r In Intersect(Target, [L7:L31])
            If r = "" Then
                r(, -9).MergeArea.ClearContents
            Else
                r(, -9) = WorksheetFunction.IfError(Application.VLookup(r, _
                    Sheets("data_base").Columns("a:b"), 2, False), "v.n.d")
            End If
        Next
        Application.EnableEvents = True
    End Sub
    Last edited by jindon; 10-24-2020 at 09:07 AM. Reason: Fixed a typo

  6. #6
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    475

    Re: VLOOKUP with macro

    In L7:L31 there is nothing
    Attached Files Attached Files

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,596

    Re: VLOOKUP with macro

    It uses Change event, like your code, so it only works when you change any cell in L7:L31 (got typo in my code, L2:L31)

    Enter any thing in blank cell, so that you will see.

  8. #8
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    475

    Re: VLOOKUP with macro

    I'm probably doing something wrong.
    I did as you say (I hope) but it doesn't change.
    Write in B7: B31 the result in cells L7: L31
    Attached Files Attached Files

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,596

    Re: VLOOKUP with macro

    Try
    1) Clear L7:L31 in S1 sheet
    2) Enter 123460 in any cell in L7:L31
    3) You should see the result in Col.B in S1 sheet.

    Above is working here.

  10. #10
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    475

    Re: VLOOKUP with macro

    tried several times in B7: B31 nothing comes out

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,596

    Re: VLOOKUP with macro

    No result, no error?

    Don't know why...

    try change to
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim r As Range, c As Range
        If Intersect(Target, [L7:L31]) Is Nothing Then Exit Sub
        Application.EnableEvents = False
        For Each r In Intersect(Target, [L7:L31])
            If r = "" Then
                r(, -9).MergeArea.ClearContents
            Else
                Set c = Sheets("data_base").Columns(1).Find(r, , , 1)
                If c Is Nothing Then
                    r(, -9) = "v.n.d"
                Else
                    r(, -9) = c(, 2)
                End If
            End If
        Next
        Application.EnableEvents = True
    End Sub
    Last edited by jindon; 10-24-2020 at 09:48 AM.

  12. #12
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    475

    Re: VLOOKUP with macro

    I don't know nothing comes out of me

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,596

    Re: VLOOKUP with macro

    I wonder if Events are disabled.
    Try the attached.
    Attached Files Attached Files

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,596

    Re: VLOOKUP with macro

    See my previous post.

  15. #15
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    475

    Re: VLOOKUP with macro

    tried # 12 doesn't work on me

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,596

    Re: VLOOKUP with macro

    I can not test this.
    Not sure if you need to change semi-column ";" to a comma "," in the formula.
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim r As Range
        If Intersect(Target, [L7:L31]) Is Nothing Then Exit Sub
        Application.EnableEvents = False
        For Each r In Intersect(Target, [L7:L31])
            With r.Offset(, -10)
                .Formula = "=SE(L8="""";"""";SE(VAL.NON.DISP(CERCA.VERT(L8;data_base!$A$2:$B$1500;2;0))" & _
                    ";""v.n.d."";CERCA.VERT(L8;data_base!$A$2:$B$1500;2;0)))"
                .Value = .Value
            End With
        Next
        Application.EnableEvents = True
    End Sub

  17. #17
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    475

    Re: VLOOKUP with macro

    I shut down and restarted the computer, now everything works.
    Thanks jindon

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,596

    Re: VLOOKUP with macro

    Yeah, it should...

    Please don't forget to mark the thread as [SOLVED].

  19. #19
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    475

    Re: VLOOKUP with macro

    I moved the data_base to test and changed here

    r(, -9) = WorksheetFunction.IfError(Application.VLookup(r, _
    Sheets("data_base").Columns("D:I"), 5, False), "v.n.d")

    but non work, where am i wrong?
    Attached Files Attached Files

  20. #20
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,596

    Re: VLOOKUP with macro

    5 should be 6
                r(, -9) = WorksheetFunction.IfError(Application.VLookup(r, _
                    Sheets("data_base").Columns("D:I"), 6, False), "v.n.d")

  21. #21
    Forum Contributor
    Join Date
    08-27-2019
    Location
    europa
    MS-Off Ver
    excel 2007
    Posts
    475

    Re: VLOOKUP with macro

    Thanks you jindon

  22. #22
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    81,485

    Re: VLOOKUP with macro

    Administrative Note:

    Unfortunately, this is a duplicate thread, and you are allowed only ONE thread per issue here.

    Please see Forum Rule #5 about thread duplication.

    I am closing this thread, but you may continue here in the original thread: https://www.excelforum.com/excel-pro...e-vlookup.html
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] IF(ISERROR(VLOOKUP,"",VLOOKUP) in macro produces run-time error 1004
    By doleenovodno in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-01-2018, 09:00 AM
  2. Replies: 3
    Last Post: 07-10-2014, 10:56 AM
  3. [SOLVED] Macro- adding Vlookup to a macro
    By TERRI LEE in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 05-06-2013, 02:28 AM
  4. [SOLVED] Adapt A Sumif Macro to A Vlookup Macro
    By timbo1957 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-30-2012, 06:22 PM
  5. Complex Vlookup/ VBA function or Macro Needed For "if contains text then vlookup"
    By alfykunable in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-09-2012, 03:48 AM
  6. Applying Vlookup using a Macro(Vlookup+Macro) in Excel 2007
    By Sumku in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-03-2009, 08:58 PM
  7. Replies: 1
    Last Post: 02-26-2005, 02:06 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