Excel奇数行赋予价值 [英] Excel odd rows give value
问题描述
我正在尝试为特定列/范围内的所有奇数单元格分配一个值.到目前为止,我从另一个问题中获取了以下代码,但它不起作用:
I am trying to assign a value to all the odd cells in a particular column/range. So far I have the following code taken from another question, but it isnt working:
Sub changeClass()
Dim r As Range
Set r = Range("B16").End(xlDown) 'set the range the data resides in
For i = 1 To r.Rows.Count 'merge step
If i Mod 2 = 1 Then 'this checkes to see if i is odd
r.Cells.Value = "value"
End If
Else
r.Cells.Value = "value2"
Next i
End Sub
基本上,我需要为B列中从单元格16到最后一个包含数据的单元格中的每个单元格添加一个值.在偶数行上,该值将是一回事,奇怪的是,将会是另一个.
Basically I need it to add in a value for every cell in the B column from cell 16 down to the last cell i nthe column which has data in. On the even rows the value will be one thing, on the odd it will be another.
非常感谢!
推荐答案
尝试一下,我认为它不起作用,因为您没有访问循环中的每个单元格.在下面的宏中,我使用"rng"表示整个单元格范围,并使用"r"表示循环的每个增量中的单个单元格.
Try this out, I believe it is not working, because you are not acessing each individual cell inside your loop. In the following macro i use 'rng' to represent the entire range of cells, and 'r' to represent a single cell in each increment of the loop.
Sub changeClass()
Dim rng As Range
Dim r As Range
Set rng = Range(Cells(16,2),Cells(16,2).End(xlDown))
For i = 1 To rng.Rows.Count
Set r = rng.Cells(i)
If i Mod 2 = 1 Then ' You may want to test if it is odd based on the row number (depends on your problem...)
r.Value = "Odd Value"
Else
r.Value = "Even Value"
End If
Next i
End Sub
这篇关于Excel奇数行赋予价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!