Results 1 to 10 of 10

Thread: Problem : Excel formula using VB code

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    Problem : Excel formula using VB code

    Hi All,

    I've got a problem with excel.

    I am facing this problem while I enter formula in the excel sheet using vb code. I have selected a cell and then I use the code,

    activecell.formulaR1C1 = "=vlookup(blah,blah...)"

    and then I fill this formula to the desired range of cells.

    What happens is....the formula is written in the cells as a text and the formula is not picking the value from the page mentioned in it.

    In the excel sheet, in the desired cell (eg H1) the content remains as =vlookup(blah,blah...) instead of the value.

    Can anyone help me out of this???

  2. #2
    Addicted Member
    Join Date
    Apr 2009
    Location
    Toronto, Ontario
    Posts
    242

    Re: Problem : Excel formula using VB code

    Welcome to the forum!

    Give us a full example. "blah, blah, blah" doesn't tell us if you correctly coded the formula! Also, instead of Activecell try using a range:
    Code:
    Worksheets("Sheet1").Range("B1").FormulaR1C1 = <formula here>
    It's just better practice to use a specific range instead of Activecell.

    Another suggestion: use a different, simpler formula i.e. =SUM and see if you get the same result. For example, this works fine for me when A1 through A3 have 1,2,3 respectively, giving me a value of 6:
    Code:
    Worksheets("Sheet1").Range("B3").FormulaR1C1 = "=SUM(r1C1:r3C1)"
    Just thought of something else, check the format of the cell. Is it "Text"? If so, formulas won't work in any cell that is "Text" format.

    Sorry for the train of thought post, but I hope it helps.
    -EM
    ---
    REMEMBER: If your issue is resolved, use the Thread Tools menu to set it as such, and be sure to rate the posts that help you the most!


    Just because I was jealous of g4hsean!

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem : Excel formula using VB code

    Code:
    activecell.formulaR1C1 = "=vlookup(blah,blah...)"
    Welcome to the forums Syan

    I guess this is what you are trying?

    Code:
    ActiveCell.Formula = "=vlookup(blah,blah...)"
    If it still shows you the formula rather than calculating it then do the following... (Since I don't know whether you are using Excel 2003 or 2007, I am including solution for both versions)

    1) Excel 2003

    Click on Tool~~>Options~~>View Tab (see picture below)



    Uncheck the checkbox "Formulas" under "Window options"

    2) Excel 2007

    Under Formula Tab, unselect the option "Show Formulas"

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Problem : Excel formula using VB code

    The cell was formatted as Text.
    Change it's NumberFormat to General, it will accept formula as formula not Text.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    Re: Problem : Excel formula using VB code

    Guys,

    Thanks for your replies...

    I used the formula,
    Code:
    activecell.formular1c1 = "=vlookup(B:B,'EMP DETAILS'!B:E,4,false)"
    ;;;;;;;;;;;;;;;

    I even tried using
    Code:
    activesheet.range("H3").value = application.worksheetfunction.vlookup([B3],worksheet("EMP DETAILS").range("B:E").value,4,false)
    but I get an error called

    unable to get the vlookup property of worksheet function.....

    Actually what I am trying to do is to fill in values from a sheet into other sheet.

    Is there any other possibles that you guys could suggest..

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem : Excel formula using VB code

    I recommend going through Post # 3 again
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    Re: Problem : Excel formula using VB code

    koolsid,

    Its already unchedked....

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    Re: Problem : Excel formula using VB code

    Even the format of the cell was "General"..

  9. #9
    Addicted Member
    Join Date
    Mar 2005
    Posts
    222

    Re: Problem : Excel formula using VB code

    Hi Syan,

    What happens when you run this:
    Code:
    With ActiveCell
        .NumberFormat = "General"
        .Formula = "=vlookup(B:B,'EMP DETAILS'!B:E,4,false)"
    End With

  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem : Excel formula using VB code

    Quote Originally Posted by Syan View Post
    koolsid,

    Its already unchedked....
    I recommend going through Post # 3 once again
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width