+ Reply to Thread
Results 1 to 10 of 10

Populate Hlookup formula through VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    04-14-2019
    Location
    Singapore
    MS-Off Ver
    Office 365
    Posts
    20

    Populate Hlookup formula through VBA

    Hi,

    I have 2 sheets - first sheets are "TestCases" sheet where data is in horizontal format and another sheet is TCTemplate sheet where data is in vertical format. In test cases I am populating 5 field after A python program run and want that status fail pass is updated automatically in TC template looking the test id Horizontally. I want Hlookup formula only and not copy paste the values.

    So how can I populate the Hlookup formula in TCTemplate sheet where Lookup value comes from vertical range and lookup range in horizontal range incrementing by every column.

    Please see attached Excel sheet. I was tryomg with macro but not able to resolve. Pls help
    Attached Files Attached Files

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

    Arrow Re: Populate Hlookup formula through VBA


    Hi,

    yes VBA can write to cells any Excel formula but as your forgot to share your working formula …

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

    Re: Populate Hlookup formula through VBA

    saxena_mk,
    try
    Sub test()
        With Sheets("TCTemplate")
            With .Range("c2", .Range("b" & Rows.Count).End(xlUp)(1, 2))
                .Formula = "=iferror(hlookup(""*""&b2,'testcases'!$e$3:$r$7,5,false),"""")"
            End With
        End With
    End Sub

  4. #4
    Registered User
    Join Date
    04-14-2019
    Location
    Singapore
    MS-Off Ver
    Office 365
    Posts
    20

    Re: Populate Hlookup formula through VBA

    Quote Originally Posted by jindon View Post
    saxena_mk,
    try
    Sub test()
        With Sheets("TCTemplate")
            With .Range("c2", .Range("b" & Rows.Count).End(xlUp)(1, 2))
                .Formula = "=iferror(hlookup(""*""&b2,'testcases'!$e$3:$r$7,5,false),"""")"
            End With
        End With
    End Sub
    Excellent Jindon.

    Another 2 issues I am sure you can help with

    1. The test Cases sheet will be dynamic I mean the name will change as such I need to use a parametrized sheet name. I tried using the sheet name in a variable ws as sunder and it is working fine in the sample excel but when i put it in my actual code it gives " Application defined ...." error

    Sub test()
    ws1 = "TestCases"
    With Sheets("TCTemplate")
    With .Range("c2", .Range("b" & Rows.Count).End(xlUp)(1, 2))
    .Formula = "=iferror(hlookup(""*""&b2," & ws1 & "!$e$3:$r$7,5,false),"""")"
    End With
    End With
    End Sub

    2. Another issue I need to use cell property in target so that I can use the variables for columns number Lookup range as the columns will be dynamic I mean will be either reducing or adding with different sheets being generated.. so instead of using $e$3:$r$7 how can we use cell range?

    Thanks

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

    Re: Populate Hlookup formula through VBA

    I don't really understand how you want it exactly,
    Sub test()
        Dim ws1 As String, myRng As String
        ws1 = "TestCases"
        myRng = Sheets(ws1).Range("e3:r7").Address
        With Sheets("TCTemplate")
            With .Range("c2", .Range("b" & Rows.Count).End(xlUp)(1, 2))
                .Formula = "=iferror(hlookup(""*""&b2,'" & ws1 & "'!" & myRng & ",5,false),"""")"
            End With
        End With
    End Sub

  6. #6
    Registered User
    Join Date
    04-14-2019
    Location
    Singapore
    MS-Off Ver
    Office 365
    Posts
    20

    Re: Populate Hlookup formula through VBA

    Hi Jindon,

    Your solution worked but a small issue still remains:

    Sorry that I could not explain it clearly but please see below code wherein I used cell property so that I can use my Variable in MyRng .

    Now the first problem is solved but the other problem is I want to use Variable tRng in formula (b2) as under but I could not concatenate the same and it is giving one or the other issue . Can you pls help. First commented formula is working fine but the non commented one is not working. The reason is b2 in formula is also dynamic. Hope you understand . Thanks


    Sub test1()
        Dim ws1 As String, myRng As String
        
        
        ws1 = "TestCases"
        colmncnt = ThisWorkbook.Sheets(ws1).Cells(3, Columns.Count).End(xlToLeft).Column
        
        With Sheets(ws1)
            myRng = .Range(.Cells(3, 5), .Cells(7, colmncnt)).Address 'Sheets(ws1).Range("e3:r7").Address
        End With
        
        '//USe Variable for
        NewRow =2
        tRng = Range("b" & NewRow).Address(False, True)
        Debug.Print tRng
        
        With Sheets("TCTemplate")
            With .Range("c2", .Range("b" & Rows.Count).End(xlUp)(1, 2))
    '            .Formula = "=iferror(hlookup(""*""&b2,'" & ws1 & "'!" & myRng & ",5,false),"""")"
                .Formula = "=iferror(hlookup(""*"" " & tRng & " ,'" & ws1 & "'!" & myRng & ",5,false),"""")"
            End With
        End With
    
    End Sub
    Attached Files Attached Files
    Last edited by saxena_mk; 10-11-2021 at 08:02 AM.

  7. #7
    Registered User
    Join Date
    04-14-2019
    Location
    Singapore
    MS-Off Ver
    Office 365
    Posts
    20

    Re: Populate Hlookup formula through VBA

    HI Jindon,

    The problem is solved. The issue was I was not inputting another & inside quotes and the correct formula was to be "=iferror(hlookup(" & Chr(34) & "*" & Chr(34) & "&" & tRng & ",'" & ws1 & "'!" & myRng & ",5,false),"""")" .

    Thanks for all the support.

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

    Re: Populate Hlookup formula through VBA

    Quote Originally Posted by saxena_mk View Post
    HI Jindon,
    Thanks for all the support.
    You ae welcome.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  9. #9
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,324

    Re: Populate Hlookup formula through VBA

    Administrative Note:

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code] [/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, 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

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    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. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  10. #10
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,324

    Re: Populate Hlookup formula through VBA

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

+ 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. Replies: 2
    Last Post: 07-22-2020, 09:55 AM
  2. Looking for help with what I think is a HLOOKUP formula
    By jamesemaj89 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-18-2016, 05:15 AM
  3. [SOLVED] HLOOKUP Formula Help
    By dmcf123 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-21-2014, 09:52 AM
  4. HLOOKUP Help. HLOOKUP links to Drop down list problems
    By finalmike in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-02-2013, 07:08 PM
  5. Replies: 3
    Last Post: 03-13-2012, 03:16 PM
  6. HLOOKUP in HLOOKUP, Base Estimate Table in Excel
    By gqdallas in forum Excel General
    Replies: 20
    Last Post: 10-19-2009, 01:41 PM
  7. Replies: 2
    Last Post: 10-08-2009, 09:51 PM

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