使用excel宏查找给定总和和产品的数字 [英] Find numbers given sum and product using excel macro

查看:126
本文介绍了使用excel宏查找给定总和和产品的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查找给定金额和产品的数字



我尝试过:



  Sub  test()
Dim sum, prod,outcounter,incounter
prod = 1
对于 outcounter = 1 Sheet1.Cells.Range( A1)。值
对于 incounter = 1 Sheet1.Cells.Range( A1 )。价值
sum = outcounter + incounter
prod = outcounter * incounter


If sum = Sheet1.Cells.Range( A1 )。值然后
如果 prod = Sheet1.Cells.Range(< span class =code-string> A2)。值然后
Sheet1.Cells.Range( B1)。Value = outcounter
Sheet1。 Cells.Range( B2)。Value = incounter
GoTo 发​​现
结束 如果
结束 如果

下一步
下一步
找到:
结束 Sub

解决方案

< blockquote>这是宏的完全优化版本。 :-O

  Sub  test()
结束 Sub



如果您在编程之前只使用过您的大脑,那么您应该给予发现问题只是解决二次方程式问题。



我让你做的工作是找到如何得到方程式和求解公式。



给downvoter的消息没有提供解决方案:

进行数学分析,你会发现不需要宏。

解决方案只是4个公式(2个解决方案和2个变量),适合单元格公式。

由于这是Homework,只需提供完整的解决方案就能让我获得更多的downvotes

Find numbers given sum and product

What I have tried:

Sub test()
 Dim sum, prod, outcounter, incounter
     prod = 1
For outcounter = 1 To Sheet1.Cells.Range("A1").Value
    For incounter = 1 To Sheet1.Cells.Range("A1").Value
        sum = outcounter + incounter
        prod = outcounter * incounter
        
        
        If sum = Sheet1.Cells.Range("A1").Value Then
            If prod = Sheet1.Cells.Range("A2").Value Then
                Sheet1.Cells.Range("B1").Value = outcounter
                Sheet1.Cells.Range("B2").Value = incounter
            GoTo found
            End If
        End If
        
    Next
Next
found:
End Sub

解决方案

Here is a fully optimized version of your macro. :-O

Sub test()
End Sub


If only you had used your brain before programming, you should gave discovered that the problem is just about solving a quadratic equation.

I let you do the work of finding how to get the equation and the formula for solution.

Message to downvoter that don't give a solution:
Do the mathematical analyze and you will see that there is no need of a macro.
the solution is just 4 formulas (2 solutions and 2 variables) that fit in cells formulas.
And since this is Homework, just giving the full solution would earn me even more downvotes.


这篇关于使用excel宏查找给定总和和产品的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆