Subtracting from an offset cell based on certain conditions

Moonbeam111

Board Regular
Joined
Sep 24, 2018
Messages
73
Office Version
  1. 365
  2. 2010
I am trying to figure this out. I have range("I22:I25"). In that range I would like to search for a particular set of words. Lets simply call them: "Example1", "Example2",and "Example3"

I am wanting to run a macro that looks for those words (example1, example2 etc....). And if it matches to subtract by 1 the number that is to the left of it. But only subtract if there is actually a number there, not if its a blank cell.

Might sound confusing but here is an example.

Macro will run. When it runs it will look for the words Example1, 2 or 3 in ColumnB. If it finds the word it then checks to the left of the cell in ColumnA and checks to see if its blank or not. If its not blank it will then subtract 1 from it.

So if I ran the macro right now. The finished result in ColumnA would be: stays empty, 2, stays empty.
ColumnAColumnB
Example1
3Example2
Example3
 
Could i get a little help with that?
Sure, it requires just a few minor tweaks to change the range and swap the column order.

VBA Code:
Sub Subtract_1_v3()
  Dim a As Variant
  Dim i As Long
  
  With Range("D22:E25")
    a = .Value
    For i = 1 To UBound(a)
      Select Case a(i, 1)
        Case "Example1", "Example2", "Example3"
            a(i, 2) = a(i, 2) - 1
      End Select
      If a(i, 2) <= 0 Then a(i, 2) = Empty
    Next i
    .Value = a
  End With
End Sub
 
Upvote 0
Solution

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You are welcome.
However, to help future readers I think that you should swap the 'Mark as solution' to the post that answered your original question, not your follow-up question.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top