简体   繁体   English

根据单元的结果运行的VBA宏

[英]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. 如果A1是1111100,则运行X宏;如果其1000000,则运行此宏,等等。我看过“ Case Select”,但是我对这件事的缺乏了解使我觉得这不是我想要的。

Any ideas? 有任何想法吗? :/ :/

Thank you in advanced. 在此先感谢您。

JB JB

you can combine the two types, and yes, a Case Select is the easiest to read and maintain. 您可以将两种类型结合使用,是的, Case Select是最容易阅读和维护的。

Here's example code that runs different routines depending on what is in A1: 以下示例代码根据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 请注意,我也禁用/启用事件,因此不会在每次更改单元格时触发

There are two main types of Excel macros Excel宏有两种主要类型

1- Those that are written to perform actions, change data, etc. 1-为执行动作,更改数据等而编写的代码。

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) 2-以及那些用于执行计算并将值返回到某些单元格的变量(用作自定义公式,这些公式未内置于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 第一种类型只能通过单击表单上的按钮,在Excel中调用“宏”窗口并选择要运行的宏的名称来触发以开始执行。

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. 在第二种类型中,Excel将忽略任何试图修改其他单元格内容,对工作表,工作簿执行操作的代码,或不限于包含自定义宏公式的单元格的任何其他操作。

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. 如果要编写第二种类型的宏,则可以这样做,但是代码将仅限于单个单元格。

Yes would need to loop through them. 是的,将需要遍历它们。

You could replace the "cas" subs in following with your implementation of VBA for that case. 在这种情况下,可以使用VBA的实现替换下面的“ cas”子项。

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". 因此,具有值1,2,3的单元格A1至A3将连续弹出msgboxes“ hi”“ ha”“ ho”。

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. 当我没有其他要停止宏的数据的单元时,通过使用Intergers停止此操作,并在代码中检测到许多“ 1”,然后使用“ For”命令根据需要复制数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM