[英]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.