简体   繁体   English

一个程序,它将在最后一行中找到具有最小值的列,并将其在第一行中的值分配给变量? [Excel VBA]

[英]A program that will find the column with a minimum value in its last row and assign its value in the first row to a variable? [Excel VBA]

I got a lot of very helpful advice in the last thread I posted about this, but it's not working out too well, so I thought I'd get a little more help on this. 在我发布的关于此问题的最后一个帖子中,我得到了很多非常有用的建议,但它的效果并不好,所以我想我会得到更多的帮助。

So I have a chart that looks something like this. 所以我有一个看起来像这样的图表。 Assume that the top left value, 1, is in cell A1: 假设左上角值1在单元格A1中:

x=    1    2    3    4    5    6    7    8

      4    3    2    1    2    3    4    5

      9    8    7    6    7    8    9    10

      8    7    6    5    4    3    2    1

Sum= 21   18   15   12   13   14   15    16

Row one is made up of x values from 1 to 8. Rows two, three, and four are values resulting from using the x-values in row one in an equation. 第一行由1到8的x值组成。行2,3和4是使用等式中第一行中的x值得到的值。 Row five is the sum of rows two, three, and four. 第五行是第2,3行和第4行的总和。

What I need my program to do is, using VBA, to go through the Sum row, row five, and detect the smallest value. 我需要我的程序做的是,使用VBA,遍历Sum行,第五行,并检测最小值。 In this case it would be 12. It then should assign the x-value for that column to a variable, X-Min. 在这种情况下它将是12.然后它应该将该列的x值分配给变量X-Min。 Lastly, It should assign the x-values to the left and right of X-Min to their own variables, X-Left and X-Right. 最后,它应该将X-Min左侧和右侧的x值分配给它们自己的变量X-Left和X-Right。

So for this example, it would go through the sum row and find the smallest value is 12. So for that column, it would go to row 1, and assign the value 4 to X-Min. 因此,对于此示例,它将通过sum行并找到最小值为12.因此对于该列,它将转到第1行,并将值4分配给X-Min。 It would then offset to the left one and assign X-Left = 3, and then offset to the right and assign X-Right = 5. 然后它将向左偏移并指定X-Left = 3,然后向右偏移并指定X-Right = 5。

This seems like it would be really simple, but I'm having a lot of problems. 这看起来很简单,但我遇到了很多问题。

From my last post, I found that using the MIN() function on the sum row will find me the smallest value. 从我上一篇文章中,我发现在sum行上使用MIN()函数会找到最小的值。 The MATCH() function can then give the column number that value is then. 然后,MATCH()函数可以给出该值的列号。 At this point, since I know that the x-values are all in row one, I can use the ADDRESS() function and using the results from the MATCH() function, have the address of the value I want for X-Min. 此时,由于我知道x值全部在第一行,我可以使用ADDRESS()函数并使用MATCH()函数的结果,获得我想要的X-Min值的地址。 Using the INDIRECT() function, I can then have the value of that address assigned to X-Min. 使用INDIRECT()函数,我可以将该地址的值分配给X-Min。

There are a few issues I'm having. 我遇到了一些问题。 First, I can't get this to actually work in VBA. 首先,我无法让它在VBA中实际工作。 I keep getting data mismatch errors. 我不断收到数据不匹配错误。 Secondly, I'm not sure how to use this method to also find and assign the values for X-Left and X-Right. 其次,我不确定如何使用此方法来查找和分配X-Left和X-Right的值。 I was thinking of using the address output, and then offsetting to the left and right, but I keep getting data mismatch errors there too. 我正在考虑使用地址输出,然后向左和向右偏移,但我也在那里得到数据不匹配错误。

I guess my main problem is that I'm not sure how to actually make this output stuff, and once it does, use the output in the way I need to. 我想我的主要问题是我不确定如何实际输出这些东西,一旦它确实如此,就按照我需要的方式使用输出。

I'm very new to VBA, and a lot of this is starting to go over my head a bit. 我对VBA很新,其中很多都开始过头了。 I understand what each part does individually, but when they come together and give me errors, I don't entirely understand why. 我理解每个部分单独做什么,但当他们走到一起并给我错误时,我并不完全理解为什么。

For example, I'm trying to get this to work in the excel spreadsheet before even using VBA, just so I know what I'm doing. 例如,我试图在使用VBA之前在excel电子表格中使用它,只是因此我知道我在做什么。 Putting in: 投入:

=CELL(ADDRESS(5,MATCH(MIN(A5:H5),A5:H5,0)))

into the formula for a cell is giving me a #VALUE! 进入一个单元格的公式给了我一个#VALUE! error. 错误。

I'm just very confused and would appreciate help! 我只是很困惑,很感激帮助!

I just saw that you have posted a new question for this. 我刚刚看到你发布了一个新问题。 In continuation to my comment in the previous solution and to accommodate your new request, you can modify the old code to do this 继续我在上一个解决方案中的评论并满足您的新请求,您可以修改旧代码来执行此操作

Sub Sample()
    '~~> This will give you the value from row 1 in the same column
    MsgBox Cells(1, Range(Application.Evaluate("=CELL(""address"",INDEX(A4:H4,MATCH(MIN(A4:H4),A4:H4,0)))")).Column).Value
    '~~> This will give you the value from row 1 in immediate left column
    MsgBox Cells(1, Range(Application.Evaluate("=CELL(""address"",INDEX(A4:H4,MATCH(MIN(A4:H4),A4:H4,0)))")).Column - 1).Value
    '~~> This will give you the value from row 1 in immediate right column
    MsgBox Cells(1, Range(Application.Evaluate("=CELL(""address"",INDEX(A4:H4,MATCH(MIN(A4:H4),A4:H4,0)))")).Column + 1).Value
End Sub

FOLLOWUP 跟进

Sub Sample()
    Dim Counter As Long

    Counter = Application.InputBox(Prompt:="Please enter a number", Type:=1)

    If Counter = False Or _
    Counter > ActiveSheet.Rows.Count -2 Then Exit Sub

    '~~> This will give you the value from row 1 in the same column
    MsgBox Cells(1, Range(Application.Evaluate("=CELL(""address"",INDEX(" & Counter + 2 & ":" & Counter + 2 & _
    ",MATCH(MIN(" & Counter + 2 & ":" & Counter + 2 & ")," & Counter + 2 & ":" & Counter + 2 & ",0)))")).Column).Value

    '~~> This will give you the value from row 1 in immediate left column
    '~~> You will have to put an error check here if the current column is 1
    MsgBox Cells(1, Range(Application.Evaluate("=CELL(""address"",INDEX(" & Counter + 2 & ":" & Counter + 2 & _
    ",MATCH(MIN(" & Counter + 2 & ":" & Counter + 2 & ")," & Counter + 2 & ":" & Counter + 2 & ",0)))")).Column - 1).Value

    '~~> This will give you the value from row 1 in immediate right column
    '~~> You will have to put an error check here if the current column is the same as total columns count
    MsgBox Cells(1, Range(Application.Evaluate("=CELL(""address"",INDEX(" & Counter + 2 & ":" & Counter + 2 & _
    ",MATCH(MIN(" & Counter + 2 & ":" & Counter + 2 & ")," & Counter + 2 & ":" & Counter + 2 & ",0)))")).Column + 1).Value
End Sub

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

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