简体   繁体   中英

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:

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. Row five is the sum of rows two, three, and four.

What I need my program to do is, using VBA, to go through the Sum row, row five, and detect the smallest value. In this case it would be 12. It then should assign the x-value for that column to a variable, 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.

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. It would then offset to the left one and assign X-Left = 3, and then offset to the right and assign 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. The MATCH() function can then give the column number that value is then. 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. Using the INDIRECT() function, I can then have the value of that address assigned to X-Min.

There are a few issues I'm having. First, I can't get this to actually work in 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. 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. 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. Putting in:

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

into the formula for a cell is giving me a #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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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