从excel中提取非零值

时间:2017-07-27 11:29:32

标签: excel excel-vba simplex-algorithm vba

我已经将这张桌子放在菜单中,并且具有营养价值。然后,我得到另一个表,其中每行包含一行,每个项目消耗的数量在下面的行中。然后我使用excel中的求解器工具来优化用餐选择,并限制用餐的卡路里量以及对我使用的营养价值的某些限制。

当我通过求解器工具运行单纯形算法时,会发生的情况是表中消耗的数量值会发生变化,以反映在给定约束条件下应该吃的东西。

我想让它变得灵活,以便我可以更改约束并获得不同的结果,但我想要的是一种简单的方式来显示所做出的选择。目前我所拥有的是另一个选项卡到表格的索引匹配以及我随后应用过滤器的值以及为消耗的数量取消所有带有“0”的项目,但是每次运行求解器时都必须这样做。

有没有办法拉出非零并显示这些参考的项目,而不必每次都重做过滤器?

1 个答案:

答案 0 :(得分:1)

这是一个简单的例程,我用它在电子表格表中查找内容并将结果发布在同一页面上(很容易更改为在另一张表上发布)。希望这有助于或引导您朝着正确的方向前进。 (原油但有效)

Private Sub CommandButton3_Click()
    'FIND A VALUE IN THE LIST AND POST RESULTS

Dim myName, myNumber, myComp

  'Clear the results area
With Worksheets("SheetName").Range("H2:J30").ClearContents
End With
x = 2   'The row to start posting results to
y = 0

  'This is the range to search
With Worksheets("SheetName").Range("A1:D300")

   Set found = .Find(What:=y, LookIn:=xlValues,  LookAt:=xlWhole)
      If Not found Is Nothing Then
        firstAddress = found.Address
      Do

        Set ws = Sheets("SheetName")
        myName = ws.Range("A" & found.Row).Value  'Value in column A
        myNumber = ws.Range("B" & found.Row).Value 'Value in column B
        myComp = ws.Range("C" & found.Row).Value   'Value in column C 

               'I use a MsgBox at first for testing, then comment it out  when I know it's working 
               'MsgBox myName & "   " & myNumber & "    " & myComp

                'Post the results to the desired area
                ws.Range("H" & x).Value = myName
                ws.Range("I" & x).Value = myNumber
                ws.Range("J" & x).Value = myComp
            x = x + 1
        Set found = .FindNext(found)
        If found Is Nothing Then
            GoTo DoneFinding
        End If
        Loop While Not found Is Nothing And found.Address <> firstAddress
   End If
DoneFinding:
End With
Range("A2").Select
End Sub
相关问题