Home » excel » excel – Creating Plus & Minus buttons using VBA using IF THEN Statements

excel – Creating Plus & Minus buttons using VBA using IF THEN Statements

Posted by: admin May 14, 2020 Leave a comment

Questions:

I realize that I may be going down a rabbit hole here but it is worth a shot.

I am attempting to create one set of plus minus buttons in excel and have it apply to different cells on a separate sheet based on previously selected criteria.

I have searched all over and have found nothing.

Here is my example…

I have a staffing model where I want to forecast what will happen when I add or subtract employees. I am separating the employees by two types, Sales & Non-Sales. There are various titles within each type.

I am planning to reduce my Senior Sales Associate count by 2 FTE’s and reduce my Sales Associate FTEs by 1.

I want to be able to use a data validation list to select “Senior Sales Associate” and then use a subtraction button to reduce by 3. I then want to switch my data validation list to select “Sales Associate” and use the same subtraction button to reduce that count by 1.

I was attempting to use Excel VBA If THEN statements but they do not seem to be working. I will provide an example of the code I thought would work below.

Sub Plus()

   If Range("A1")= "Sales Associate" Then
       Range("L10").Value = Range("L10").Value + 1        
   End If

End Sub

I am aware that this code will not account for multiple instances like I am asking, but it was not even working with one on the test. Please let me know if this is possible! Thanks in advance!

How to&Answers:

Start from the easiest and the smallest part of the code and try to make it work.
E.g. like this one:

Sub Plus()
    Range("L10").Value = Range("L10").Value + 1
End Sub

It should work, increasing the values of L10 with 1.
Then, consider the If function. Probably there is some space at the end or something. Thus, try a simple if like this:

Sub Plus()    
   If Trim(Range("A1")) = "s" Then
       Range("L10").Value = Range("L10").Value + 1
   End If    
End Sub

The Trim() would strip the spaces from the left and the right of the checked string and would reduce the inner spaces to not more than 1. As a further check, it could be a better solution to use LCase() because “Sales Associate” is a different string than “Sales associate” and mistakes happen:

Sub Plus()

   If LCase(Trim(Range("A1"))) = "sales associate" Then
       Range("L10").Value = Range("L10").Value + 1
   End If

End Sub

At the end, if the code is in a module, then the ActiveSheet should be referred as well. Take a look at the dots before .Range():

Sub Plus()

    With Worksheets("SomeSheet")
        If LCase(Trim(.Range("A1"))) = "sales associate" Then
            .Range("L10").Value = .Range("L10").Value + 1
        End If
   End With

End Sub