JamieB
JamieB

Reputation: 247

VBA macro to run based on the outcome of a cell

I want to be able to run macros based on the outcome of the cell, for example.

If A1 is 1111100 then run X macro If its 1000000 then run this macro etc. I have had a look at "Case Select" but my lack of knowledge in this matter makes me thing that might not be what I want.

Any ideas? :/

Thank you in advanced.

JB

Upvotes: 1

Views: 6045

Answers (4)

JamieB
JamieB

Reputation: 247

Fixed it.

Done this via using Intergers to stop when I have no further cells with data to stop the macro, and detect on many '1's are within the code and copy the data as neededs using "For" commands.

Upvotes: 0

Robbert Koppenol
Robbert Koppenol

Reputation: 103

Yes would need to loop through them.

You could replace the "cas" subs in following with your implementation of VBA for that case.

Function Strange(myVal)
Select Case myVal
Case 1
    Cas1
Case 2
    Cas2
Case 3
    Cas3
End Select
End Function

Sub Cas1()
 MsgBox ("hi")
End Sub

Sub Cas2()
 MsgBox ("ha")
End Sub

Sub Cas3()
 MsgBox ("ho")
End Sub

Sub LoopThem()
Do While ActiveCell.Value <> ""
    Strange (ActiveCell.Value)
    ActiveCell.Offset(1, 0).Activate
    Loop
End Sub

So cell A1 to A3 with values 1,2,3 would consecutively pop up msgboxes "hi" "ha" "ho".

Upvotes: 0

SeanC
SeanC

Reputation: 15923

you can combine the two types, and yes, a Case Select is the easiest to read and maintain.

Here's example code that runs different routines depending on what is in A1:

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Range("A1"), Target) Is Nothing Then
    Application.EnableEvents = False
    Select Case Target.Value
        Case "A"
            FunctionWhenA
        Case 1
            ThasIsAnotherFunction
    End Select
    Application.EnableEvents = True
End If
End Sub

note that I also disable/enable events so this isn't triggered every time a cell is changed

Upvotes: 1

Ahmad
Ahmad

Reputation: 12717

There are two main types of Excel macros

1- Those that are written to perform actions, change data, etc.

2- And those that are written to perform calculations and return values to certain cells (used as customized formulas that are not built in to excel)

The first type can only be triggered to start execution by clicking a button on a form, invoking the Macros window within Excel and selecting the name of the macro to be run

The second type can be run as soon as a value of a certain cell changes (that cell must be an input for the said macro function to work with, calculate and return a certain output), and thus the returned value will be stored on another cell.

In the second type, Excel will ignore any code that tries to modify the content of other cells, perform actions on the worksheet, workbook, or any other action that is not limited to the cell contanining the formula for the custom macro.

If you intend to run a macro of the first type, and want it to be executed right after a certain value changes, then that is not possible.

If you want to write a macro of the second type, then that is possible but code will only be limited to a single cell only.

Upvotes: 0

Related Questions