+ Reply to Thread
Results 1 to 4 of 4

macro math formula dependent on variables

  1. #1
    Registered User
    Join Date
    11-04-2010
    Location
    nevada, united states
    MS-Off Ver
    Excel 2003
    Posts
    3

    macro math formula dependent on variables

    Hello everyone, I need help creating a macro with a math problem in it.

    I have variables from drop down lists in C3 and C4 Which is currently a African Sumac and Agave and the count of how many was removed that day to the left of them.

    plants removed
    Count Type
    5 AFRICAN SUMAC
    2 AGAVE



    Then i have the current inventory below it with the plant type in C8;9;10 and the current count to the left of those

    Current Inventory
    Count Type
    100 AFRICAN SUMAC
    50 ARISTOCRAT PEAR
    60 AGAVE


    Heres my question, is it possible to make a code that subtracts the removed count from the current count dependent on if the plant types match? and since i am using a large drop down list for the removed plant material would it be possible to make the removed count default to 0 at any time that the plant type is changed. The reason for this is that if there is a code to make it subtract the removed from the current i wouldnt want it to subtract automatically if the removed variable changes via the drop down list. does that make sense? Maybe i could use a module with a macro and a button procedure?

    I appreciate all and any help. I have also created a small scale sample of would i would like to do... here is the download link.

    http://wikisend.com/download/452542/Book1.xls

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: macro math formula dependent on variables

    I think this does what you want for the example. Place in the sheet module - right-click on the sheet tab, View Code, and paste in:
    Please Login or Register  to view this content.
    I've hardcoded all the ranges, but in reality I expect yours will vary.

  3. #3
    Registered User
    Join Date
    11-04-2010
    Location
    nevada, united states
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: macro math formula dependent on variables

    The Original Code that you gave me worked in my practice workbook. But when i went to go and change the Ranges and Other Values i must have done something Wrong.

    My code is as follows
    Private Sub Worksheet_Change(ByVal Target As Range)

    Dim v

    If Intersect(Target, Range("A21:B25")) Is Nothing Then Exit Sub

    If Target.Column = 2 Then
    Target.Offset(, -2) = 0
    Else
    v = Application.Match(Target.Offset(, 2), Range("B34:B38"), 0)
    If IsNumeric(v) Then
    Range("A34:A38").Cells(v) = Range("A34:A38").Cells(v) - Target
    End If
    End If

    End Sub


    All of the numerics are in the "A Column" and all variables are in the "B column"
    The plants being removed will be in Range A21:B25
    And the Inventory is Range A34:A38


    It is giving me a error and "Target.Offset(, -2) = 0" is highlighted

    Any Idea where i went wrong?
    Your original code worked so there has to be something i changed... Right?

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: macro math formula dependent on variables

    If the target column is 2 then offset 2 to the left is column zero which doesn't exist. So, I think it should be an offset of -1.

+ 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