简体   繁体   English

如何将一个单元格的值限制为另一个单元格的值?

[英]How to limit the value of one cell's value by another cell's value?

I have a table full of numbers. 我有一张满是数字的桌子。 I need a macro script, which will compare the target value with the score value. 我需要一个宏脚本,它将目标值与分数值进行比较。 In case of the score value is higher than the target value, then change the target value to the score value. 如果得分值高于目标值,则将目标值更改为得分值。

Can somebody help me? 有人能帮助我吗?

( https://imgur.com/a/xmej0fi ) https://imgur.com/a/xmej0fi

I have tried to apply the following code on one cell, but it didn't work. 我试图在一个单元格上应用以下代码,但它不起作用。

Sub check_value()
  If Cells(8, 23).Value < Cells(8, 24).Value Then
    Cells(8, 23).Value = Cells(8, 24).Value
  End If
End Sub

F2 and F3 have a scenario where the Target < Score, so we can compare the values (similar to what you've got) F2和F3有一个目标<分数的场景,所以我们可以比较这些值(类似于你得到的)

if cells(2,6).value < cells(3,6) then cells(2,6).value = cells(3,6).value

My only correction from your tested code is using rows to compare, rather than columns, which appears to be how each Skill is being assessed (eg, Target for Skill 5 is 5 (F2), while the Score is 7 (F3)). 我对您测试代码的唯一更正是使用行来比较而不是列,这似乎是每个技能的评估方式(例如,技能目标5为5(F2),而得分为7(F3))。


Note that you can use a loop to get through the whole data set by finding last column (lc) and last row (lr), such that: 请注意,您可以通过查找最后一列(lc)和最后一行(lr)来使用循环来遍历整个数据集,这样:

For j = 2 to lc 'columns
    for i = 2 to lr step 2 'rows with a step of 2 so you can do sets of score/target
        'do stuff
    next i
next j

I think this may help you: 我想这可能会对你有所帮助:

Option Explicit

Sub test()

    Dim LastRow As Long, Row As Long, Target As Long, Score As Long, LastColumn As Long, Column As Long

    With ThisWorkbook.Worksheets("Sheet1")

        LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row

        For Column = 2 To LastColumn

            For Row = 2 To LastRow Step 2

                Target = .Cells(Row, Column).Value
                Score = .Cells(Row + 1, Column).Value

                If Score > Target Then
                    .Cells(Row, Column).Value = Score
                End If

            Next Row

        Next Column

    End With

End Sub

暂无
暂无

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

相关问题 如果一个单元格包含一个值,请将另一个单元格的值移动到另一个单元格 - If a cell contains a value, move another cell's value to other cell 如何编写if语句代码来比较一张表中两个单元格的值与另一张表中的另一个单元格 - how to write an if statement code to compare two cell's value in one sheet to another cell in another sheet 根据另一个单元格的值显示值 - Display Value Based on another Cell's Value 如何将一个单元格的范围复制到另一个单元格的值中 - How to Copy a cell's range into the value of another cell Excel公式:如何将一个单元格的一部分与另一个单元格的一部分匹配并汇出匹配的单元格的值 - Excel Formula: How to match a part of one cell to the part of another and remit the matched cell's value 如果在另一列上找到一个单元格的值,如何从与同步单元格在同一行的另一列返回该单元格的值? - If a cell's value is found on another column, how to return the cell's value from another column which is on same row with the synced cell? 如何使用另一个工作表中单元格内的单元格地址更改具有 Excel vba 的单元格的值? - How to change the value of a cell with Excel vba using a cell address that's inside a cell in another worksheet? 如何根据另一个单元格值定义一个单元格中公式返回的底值? - How to define a bottom value for a formula's return in a cell based in another cell value? Excel单元格值更改在另一个单元格中插入“ S” - Excel a change in cell value inserts a “S” in another cell 根据另一个单元格的值在单元格中添加注释 - Adding a comment in a cell based on another cell's value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM