Results 1 to 12 of 12

Thread: [RESOLVED] value of formula in vba EXCEL

  1. #1

    Thread Starter
    Addicted Member sweet_dreams's Avatar
    Join Date
    Apr 2005
    Location
    Poland, Lodz
    Posts
    189

    Resolved [RESOLVED] value of formula in vba EXCEL

    Hi all,

    I have such a problem. I would like my code to depend of cells value. But problem is that in this cell there is formula which returns value. How can I make my program read this value.

    for example: In A1 i have formula if(b1>0;1,-1)

    and programmatically I would like to do sth like that

    VB Code:
    1. if range("a1").value = 1 then
    2. 'my code
    3. end if

    the problem is that when value in cell A1 comes form formula code shown above doesn't work.

    Please help.

    Regards,
    sweet_dreams
    Last edited by sweet_dreams; Mar 17th, 2006 at 10:40 AM.

  2. #2
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: value of formula in vba EXCEL

    If your formula is like that, then it won't. You need a comma instead of a semicolon.
    Otherwise, that exactly works. Unless your code is in a module, in which case you need to specify the worksheet as well as the range.

    zaza
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  3. #3

    Thread Starter
    Addicted Member sweet_dreams's Avatar
    Join Date
    Apr 2005
    Location
    Poland, Lodz
    Posts
    189

    Re: value of formula in vba EXCEL

    Hi Zaza,

    I've just made mistake writing formula here. formula works fine. but problem is with this code (it is not in module):
    VB Code:
    1. if range("a1").value = 1 then
    2. 'my code
    3. end if

    this code doesn't respond on value in cell a1

  4. #4
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: value of formula in vba EXCEL

    Type:


    VB Code:
    1. MsgBox(Worksheets("Sheet1").Range("A1").value)

    and see what you get. It works for me.

    zaza
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  5. #5

    Thread Starter
    Addicted Member sweet_dreams's Avatar
    Join Date
    Apr 2005
    Location
    Poland, Lodz
    Posts
    189

    Re: value of formula in vba EXCEL

    here is the full code

    VB Code:
    1. Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    2.  
    3. If Target.address = "$A$1" Then
    4.     If Range("a1").Value = 0 Then
    5.         Range("a2:a20").ClearContents
    6.     End If
    7. End If
    8.  
    9. End Sub

  6. #6
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: value of formula in vba EXCEL

    You are not within the Sheet module, you are in the workbook module. Hence it doesn't recognise the range because you haven't told it on which sheet to find the range. Put:

    Worksheets("Sheet1").Range...

    instead.

    Or, to use the current sheet, sh.range...

    zaza
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  7. #7

    Thread Starter
    Addicted Member sweet_dreams's Avatar
    Join Date
    Apr 2005
    Location
    Poland, Lodz
    Posts
    189

    Re: value of formula in vba EXCEL

    with Worksheets("Sheet1").Range... it also doesn't work

  8. #8
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: value of formula in vba EXCEL

    Do you have a worksheet called "Sheet1"?
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  9. #9
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: value of formula in vba EXCEL

    I'd use the Worksheet_Change event for the individual sheet instead:
    VB Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2.  
    3.     If Target.Address = "$A$1" Then
    4.         If Target.Cells(1, 1).Value = 0 Then
    5.             Target.Parent.Range("A2:A20").ClearContents
    6.         End If
    7.     End If
    8.  
    9. End Sub

  10. #10

    Thread Starter
    Addicted Member sweet_dreams's Avatar
    Join Date
    Apr 2005
    Location
    Poland, Lodz
    Posts
    189

    Re: value of formula in vba EXCEL

    Zaza, I made like that:

    VB Code:
    1. Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    2. If Worksheets(1).Range("a1") = 0 Then
    3.         Range("a2:a20").ClearContents
    4. End If
    5. End Sub

    I've just this code into SheetCalculate event and it works now fine. But thanks ZAZA for your help and attention

    regards,
    sweet_dreams

  11. #11
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: value of formula in vba EXCEL

    Quote Originally Posted by sweet_dreams
    with Worksheets("Sheet1").Range... it also doesn't work
    Workbook_Change passes the current sheet as the Sh parameter. You can also use Target.Parent to get the associated sheet.

  12. #12
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: value of formula in vba EXCEL

    Ah, did you want it do do something when you change from one sheet to another? This is not what the SheetChange event does. The sheetchange event fires whenever any cells in a sheet are changed.
    You could use the SheetActivate event.

    zaza
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

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