这是我的按钮代码,用于替换excel中选定范围内的值,
Private Sub CommandButton1_Click()
Dim a As Variant
Dim b As Variant
b = "1"
Dim example, cell As Range
Set example = Range("G5:AK5,g7:ak7,g9:ak9,g11:ak11,g13:ak13,g15:ak15,g17:ak17,g19:ak19,g21:ak21,g23:ak23,g25:ak25,g27:ak27,g29:ak29,g31:ak31,g33:ak33,g35:ak35,g37:ak37,g39:ak39,g39:ak39,g41:ak41,g43:ak43,g45:ak45,g47:ak47,g49:ak49,g51:ak51")
For Each cell In example
If 1 < cell.Value < 8 Then cell.Value = b Else If cell.Value = vbNullString
Then cell.Value = " "
Next cell
End Sub
如何使其不使用指定值填充所选范围内的空白单元格?我只知道基本的编码,谢谢大家!
答案 0 :(得分:1)
@ Marcucciboy2评论。有几个语法错误。您不需要专门检查空白(除非您尝试做其他事情?)。如果单元格中的值> 1且<8,则将单元格值重新设置为变量b的值...
Private Sub CommandButton1_Click()
Dim b As Variant
b = "1"
Dim example, cell As Range
Set example = Range("G5:AK5,g7:ak7,g9:ak9,g11:ak11,g13:ak13,g15:ak15,g17:ak17,g19:ak19,g21:ak21,g23:ak23,g25:ak25,g27:ak27,g29:ak29,g31:ak31,g33:ak33,g35:ak35,g37:ak37,g39:ak39,g39:ak39,g41:ak41,g43:ak43,g45:ak45,g47:ak47,g49:ak49,g51:ak51")
For Each cell In example
If cell.Value > 1 And cell.Value < 8 Then
cell.Value = b
End If
Next cell
End Sub
答案 1 :(得分:0)
顺便说一句,您在范围定义中有两个g39:ak39
范围,并且 example 作为变体(而不是特定范围)变暗了。
也许这将是一个更简洁的范围定义。
...
Dim example As Range, cell As Range
Set example = Range("G5:AK5")
for i=7 to 51 step 2
Set example = union(example, intersect(range("G:AK"), rows(i)))
next i
debug.print example.address(0,0)
'G5:AK5,G7:AK7,G9:AK9,G11:AK11,G13:AK13,G15:AK15,G17:AK17,G19:AK19,G21:AK21,G23:AK23,G25:AK25,G27:AK27,G29:AK29,G31:AK31,G33:AK33,G35:AK35,G37:AK37,G39:AK39,G41:AK41,G43:AK43,G45:AK45,G47:AK47,G49:AK49,G51:AK51
...