[英]Excel VBA: Cell Value from Macro
I have a Macro that sets the value of a cell from other cell values. 我有一个宏,它可以从其他单元格值中设置一个单元格的值。
So the Macro sets the value of a cell when I run the code as usual. 因此,当我照常运行代码时,宏会设置单元格的值。
But I want the Macro work like a Formula. 但是我希望Macro像公式一样工作。
So the value of a cell always changes when those cells are change. 因此,当这些单元格更改时,单元格的值始终会更改。
Is there any way to do this? 有什么办法吗?
Or Maybe a way to call the Macro from formula? 还是从公式调用宏的一种方法?
Edit: (After Comments) 编辑:(注释后)
Ok. 好。 This is what I want from the Macro. 这就是我想要的宏。
I have Hours:Minutes, and I want to SUM all of them. 我有Hours:Minutes,我想将所有这些都加起来。
The problem is I have negative Hours:Minutes that I've calculated them using the formula below: 问题是我的小时数是负数:我使用以下公式计算了它们的分钟数:
=TEXT(ABS(H10-E10),"-h:mm")
So I have something like this: 所以我有这样的事情:
08:20, 02:10, -03:20, [and so on...]
And I want the result: 我想要结果:
08 + 02 - 03 = 07
20 + 10 - 20 = 10
= 07:10 (The Result)
Because we don't have negative Time in Excel, and I have Text instead of Time, so I have to calculate them with ForLoop in VBA. 因为我们在Excel中没有负的时间,并且我有Text而不是Time,所以我必须在VBA中使用ForLoop计算它们。
This is a very simple example. 这是一个非常简单的例子。 Say your macro changes A1 based on the value in cells B1 thru D1 . 假设您的宏更改A1基于单元格B1到D1中的值。
Sub MyMac()
Range("A1").Value = Range("B1").Value + Range("C1").Value + Range("D1").Value
End Sub
We will use an event macro to detect manual changes to any of the input cells and run MyMac. 我们将使用事件宏来检测对任何输入单元格的手动更改并运行MyMac。 Put this event macro in the worksheet code area: 将此事件宏放在工作表代码区域中:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
Set r = Range("B1:D1")
If Intersect(Target, r) Is Nothing Then Exit Sub
Application.EnableEvents = False
Call MyMac
Application.EnableEvents = True
End Sub
Because it is worksheet code, it is very easy to install and automatic to use: 因为它是工作表代码,所以非常易于安装和自动使用:
If you have any concerns, first try it on a trial worksheet. 如果您有任何疑问,请先在试用版工作表上尝试一下。
If you save the workbook, the macro will be saved with it. 如果您保存工作簿,则宏将随其一起保存。 If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx 如果您在2003年以后使用Excel版本,则必须将文件另存为.xlsm而不是.xlsx
To remove the macro: 删除宏:
To learn more about macros in general, see: 要总体上了解有关宏的更多信息,请参见:
http://www.mvps.org/dmcritchie/excel/getstarted.htm http://www.mvps.org/dmcritchie/excel/getstarted.htm
and 和
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see: 要了解有关事件宏(工作表代码)的更多信息,请参见:
http://www.mvps.org/dmcritchie/excel/event.htm http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work! 必须启用宏才能使其正常工作!
NOTE: 注意:
This is only good for Manual changes to the input cells. 这仅适用于手动更改输入单元格。 If any of B1 thru D1 contained formulas, the Calculate event would be used. 如果B1到D1中的任何一个包含公式,则将使用Calculate事件。
Shortcut Notation method for getting values from range 用于从范围中获取值的快捷方式表示法
Sub getting_value()
'Static Cell-pointing
Debug.Print [a1] 'for referring Active Sheet's cell
Debug.Print [Sheet1!a1] 'for referring Sheet1's cell
'Partial Dynamic Cell-pointing
Debug.Print [a:b].Cells(1,1) 'for referring Range
Debug.Print [5:5].Cells(1,1) 'for referring Range
Debug.Print [a:b 5:5].Cells(1,1) 'for referring Intersect
Debug.Print [a1,a3].Cells(1,1) 'for referring a1 AND a3
Debug.Print [a1.a3].Cells(1,1) 'for referring a1 TO a3
Debug.Print [a1..a3].Cells(1,1) 'for referring a1 TO a3
Debug.Print [a1:a3].Cells(1,1) 'for referring a1 TO a3
'Dynamic Cell-pointing
Debug.Print [Table1[Column1]].Cells(1,1) 'for referring Table1's column1
Debug.Print [NamedRange] 'for referring Named Range (making named range dynamic by vba -> ThisWorkbook.Names.Add)
End Sub
Reference 参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.