简体   繁体   English

如何在不同的单元格上输出结果? (VBA Excel 2007)

[英]How to output the result on different cell? (VBA Excel 2007)

I have calculated some values and there should be two results. 我已经计算了一些值,应该有两个结果。 I want to display these result in two different cell. 我想在两个不同的单元格中显示这些结果。

When I have changed A1 or A2 value, excel is stopped. 当我更改了A1或A2的值时,excel将停止。 Is there anyway I can display the values? 无论如何,我可以显示这些值吗?

Sheet1 
Private Sub Worksheet_Change(ByVal Target As Range)
   Call DisplayResult 
End Sub

Module1 
Sub DisplayResult() 
     Range("A3").Value = Range("A1").Select.Value + Range("A2").Select.Value     
     Range("B3").Value = Range("B1").Select.Value + Range("B2").Select.Value 
End Sub

you don't need to use .select at all here - 您完全不需要在这里使用.select-

Worksheet Code: 工作表代码:

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Sheet1.Range("A1:B2"), Target) Is Nothing Then
    MsgBox "Hello"
End If

End Sub

Module Code: 模块代码:

Sub DisplayResult()

Sheet1.Range("A3").Value = Sheet1.Range("A1").Value + Sheet1.Range("A2").Value
Sheet1.Range("B3").Value = Sheet1.Range("B1").Value + Sheet1.Range("B2").Value

End Sub

If you put the Call DisplayResult line in your Worksheet_Change sub routine without limiting what range it should be triggered on, then it will end up going into an infinite loop (DisplayResult triggers the Worksheet_Change sub). 如果将Call DisplayResult行放置在Worksheet_Change子例程中而不限制应在其触发的范围,则它将最终陷入无限循环(DisplayResult触发Worksheet_Change子例程)。 When should the DisplayResult code be triggered? 什么时候应触发DisplayResult代码? Only when you have changed Range A1:B2 ? 仅当您更改范围A1:B2时?

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

相关问题 在没有VBA的不同像元上输出公式的结果 - Output result of formula on different cell without VBA 如何在有符号的Excel 2007 VBA中修剪单元格中的数据 - how to trim data in a cell in Excel 2007 VBA where there is a symbol 如何使用vba在Excel 2007中找到有条件格式化单元格的填充颜色值? - How do I find the fill colour value of a conditionally formatted cell in Excel 2007 using vba? 通过像元乘法打印像元输出-VBA Excel - Printing Output of Cell By Cell multiplication - VBA Excel Excel 2007-VBA-根据单元格值在范围上应用条件格式 - Excel 2007 - VBA - Apply Conditional Format on Range Based on Cell Values Excel VBA 2007中特定单元的CircleInvalid和ClearCircle方法 - CircleInvalid and ClearCircle methods for a particular cell in excel vba 2007 如何在Excel 2007中使用VBA重命名范围 - How to rename range using VBA in excel 2007 excel 2007 vba:如何引用HPageBreaks - excel 2007 vba: how to refer to HPageBreaks 使用VBA在Excel 2007中的不同单元格中填写不同的字符串值 - Fill in different string values in different cells in Excel 2007 using VBA excel或excel VBA:如何让用户单击某个单元格并将其用作与现有单元格的匹配结果 - excel or excel VBA: how can I let user click on a cell and use it as a match result to an existing cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM