+ Reply to Thread
Results 1 to 5 of 5

Run excel formula through VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    01-17-2013
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    7

    Run excel formula through VBA

    I am trying to VLOOKUP some value in 2 sheets and if the value is found then i am putting a hyperlink on the Sheet 1 cell to point it to the Sheet 2 cell.
    I have written an Excel formula for the same and it is working fine. But i am unable to convert it into a VBA formula. I tried many combinations, but i am unable to understand what i am doing wrong.
    Excel formula:


    =IF(ISERROR(VLOOKUP(RC[7],Sheet2!R1C1:R20C1,1,FALSE)),RC[7],HYPERLINK(CELL("address",INDEX(Sheet2!R1C1:R20C1,MATCH(RC[7],Sheet2!R1C1:R20C1,0))),RC[7]))

    VBA formula which i have tried:

    Sheets(4).Range("C6" & "C" & LRow1).Formula ="= IF(ISERROR(VLOOKUP(RC[7],Sheet2!R4C2:R"& Lrow2 &"C2,1,FALSE)),RC[7],HYPERLINK(CELL("&"""address"""&",INDEX(Sheet2!R4C2:"&"R"& Lrow2 &"C2,MATCH(RC[7],Sheet2!R4C2:"&"R"& Lrow2 &"C2,0))),RC[7]))"
    P.S.: Do not worry about the row and column indexes. I wrote the formula for test file and writing the vba for the master file.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Run excel formula through VBA

    When you are trying to enter a formula that uses R1C1 notation you need to use FormulaR1C1 instead of Formula.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    01-17-2013
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Run excel formula through VBA

    You mean like this:

    Sheets(1).Range("C6" & "C" & LRow1).FormulaR1C1 = "= IF(ISERROR(VLOOKUP(RC[7],Sheet2!R4C2:R" & Lrow2 & "C2,1,FALSE)),RC[7],HYPERLINK(CELL(" & """address""" & ",INDEX(Sheet2!R4C2:" & "R" & Lrow2 & "C2,MATCH(RC[7],Sheet2!R4C2:" & "R" & Lrow2 & "C2,0))),RC[7]))"
    I tried the above..but its still not working..

    If possible, can you help me in re-writing this formula into vba?

    =IF(ISERROR(VLOOKUP(RC[7],Sheet2!R1C1:R20C1,1,FALSE)),RC[7],HYPERLINK(CELL("address",INDEX(Sheet2!R1C1:R20C1,MATCH(RC[7],Sheet2!R1C1:R20C1,0))),RC[7]))
    Last edited by bagadiamohit; 03-25-2015 at 03:42 AM.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Run excel formula through VBA

    Almost, you haven't got the range reference right.
    Sheets(1).Range("C6:C" &  LRow1).FormulaR1C1 = •••

  5. #5
    Registered User
    Join Date
    01-17-2013
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Run excel formula through VBA

    Oops! That was a small one. Somehow, i missed it out. Thanks Noorie.

    Now, the code is working fine. But I got a new problem now.

    Is there anything wrong with the ""address"" which i have mentioned in the formula?

    Because whenever i am trying to run the macro, a popup is being opened and asking for File names.

    I am pasting my entire code here:

    Public Sub hyperlinks()
    
    Sheets(1).Select
    Sheets(1).Range("C6").Select
    LRow1 = Sheets(1).UsedRange.Rows.Count
    Sheets(1).Range("C6" & ":" & "C" & LRow1).Select
    Selection.Copy
    Sheets(4).Select
    Sheets(4).Range("I1").PasteSpecial xlPasteValues
    
    Sheets(2).Select
    Sheets(2).Range("B4").Select
    Lrow2 = Sheets(2).UsedRange.Rows.Count
    Sheets(2).Range("B4" & ":" & "B" & Lrow2).Select
    Selection.Copy
    Sheets(4).Select
    Sheets(4).Range("J1").PasteSpecial xlPasteValues
    
    'Dim Rows As Integer
    Lrow3 = Sheets(4).Range("I1").End(xlUp).Row
    For Rows1 = 1 To Lrow3
    Sheets(1).Range("C" & Lrow3).FormulaR1C1 = "= IF(ISERROR(VLOOKUP(Sheet4!R" & Lrow3 & "C9,Sheet2!R4C2:R" & Lrow2 & "C2,1,FALSE)),Sheet4!R" & Lrow3 & "C9,HYPERLINK(CELL(""address"",INDEX(Sheet2!R4C2:" & "R" & Lrow2 & "C2,MATCH(Sheet4!R" & Lrow3 & "C9,Sheet2!R4C2:" & "R" & Lrow2 & "C2,0))),Sheet4!R" & Lrow3 & "C9))"
    Next Rows1
    
    End Sub

+ 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: 03-21-2014, 11:43 AM
  2. Macro: Applying formula to multiple cells in excel (formula editing)
    By city in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-27-2012, 06:41 AM
  3. Replies: 0
    Last Post: 03-19-2012, 07:32 PM
  4. Replies: 1
    Last Post: 03-04-2012, 12:03 AM
  5. [SOLVED] Excel 2002 formula displayed not value formula option not checked
    By Dean in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-28-2006, 10:35 AM

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