+ Reply to Thread
Results 1 to 8 of 8

New to VBA

  1. #1
    Registered User
    Join Date
    06-13-2013
    Location
    York
    MS-Off Ver
    Excel 2003
    Posts
    24

    New to VBA

    Hi I have used VBA and visual basic before

    I want a macro that changes the value of a cell when another cell is updated.

    I.E. Cell C2 = (A2 + B2)

    If the value in cell C2 is greater than 10 Then text in Cell D2 could say Over limit.

    I have this code in a module

    Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$C$2" Then
    If (C2.Value > 10) Then
    D2.Value = "Over limit"
    Else
    D2.Value = "Okay"
    End If
    End If
    End Sub

    Am I on the right track here.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: New to VBA

    Check out Worksheet_Calculate instead

  3. #3
    Registered User
    Join Date
    06-13-2013
    Location
    York
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: New to VBA

    Ok I have googled that and found nothing. I have changed the code to

    Private Sub Worksheet_Calculate(ByVal Target As Range)
    If Target.Address = "$C$2" Then
    If (C2.Value > 10) Then
    D2.Value = "Over limit"
    Else
    D2.Value = "Okay"
    End If
    End If
    End Sub


    But don't I need to link the function to cell C2?

    I am new to this.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: New to VBA

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-13-2013
    Location
    York
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: New to VBA

    Well I have put that code into the module1 and nothing is happening. Do I need a trigger event of some kind. The code is not running.

    I can put a button on the sheet and run code that way
    Last edited by otuatail; 08-18-2016 at 08:32 AM.

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: New to VBA

    It goes in the worksheet module, right click the sheet tab and select view code.

  7. #7
    Registered User
    Join Date
    06-13-2013
    Location
    York
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: New to VBA

    Ok Thanks. That works now. I did have a button on the form but when I moved it from the module to the worksheet it didn't.

  8. #8
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,933

    Re: New to VBA

    Please take a moment to read the forum rules and then amend your thread title to something descriptive of your problem - not what you think the answer might be. (think google search terms?). Once you have done this please send me a PM and I will remove this request. (Also, include a link to your thread - copy from the address bar)

    Many members search our previous posts, and thread titles play a big part of the search. I doubt anybody would do a search based on your title?

    To change a Title on your post, click EDIT POST then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    (note: this change is not optional )
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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