【发布时间】:2018-07-11 00:09:58
【问题描述】:
我试图在 Range("H2:FA2") 中找到该范围内任何值为“2”的单元格的列,然后设置该列的行(即 3,15,16,17,18 ,19,20,22,24,27,28,30,32,35,38,41,44,47,50,53,56,59,62,65,68,71,74,77,80,83 ,86) 到零值。
我尝试修改下面适用于单行的代码,但运气不佳。
我不确定使用数组的最佳方法是什么?
如果我可以根据搜索范围中的定位单元格设置一个变量来等于列名,然后使用它来循环我的数据集?
Sub test()
Dim r As Range, cell As Range, mynumber As Long
Set r = Sheets("Pending Change Order Detail").Range("H2:FA2")
mynumber = 0
For Each cell In r
If cell.Value = 2 Then
cell.Value = mynumber
End If
Next
End Sub
我最终做的是:
Sub setcolumtozero()
'setcolumtozero Macro
'set specific rows to zero if the column has a 2 in range (column,2)
Dim mycolumn As Range, cell As Range
Set mycolumn = Sheet10.Range("H2:FA2")
For Each cell In mycolumn
If cell.Value = 2 Then
Cells(3, cell.Column).Value = 0
Cells(15, cell.Column).Value = 0
Cells(16, cell.Column).Value = 0
Cells(17, cell.Column).Value = 0
Cells(18, cell.Column).Value = 0
Cells(19, cell.Column).Value = 0
Cells(20, cell.Column).Value = 0
Cells(22, cell.Column).Value = 0
Cells(24, cell.Column).Value = 0
Cells(26, cell.Column).Value = 0
Cells(27, cell.Column).Value = 0
Cells(28, cell.Column).Value = 0
Cells(30, cell.Column).Value = 0
Cells(32, cell.Column).Value = 0
Cells(35, cell.Column).Value = 0
Cells(38, cell.Column).Value = 0
Cells(41, cell.Column).Value = 0
Cells(44, cell.Column).Value = 0
Cells(47, cell.Column).Value = 0
Cells(50, cell.Column).Value = 0
Cells(53, cell.Column).Value = 0
Cells(56, cell.Column).Value = 0
Cells(59, cell.Column).Value = 0
Cells(62, cell.Column).Value = 0
Cells(65, cell.Column).Value = 0
Cells(68, cell.Column).Value = 0
Cells(71, cell.Column).Value = 0
Cells(74, cell.Column).Value = 0
Cells(77, cell.Column).Value = 0
Cells(80, cell.Column).Value = 0
Cells(83, cell.Column).Value = 0
Cells(86, cell.Column).Value = 0
End If
Next
End Sub
我会尝试这里提到的其他答案,因为我想学习更快地完成工作的方法。
【问题讨论】:
-
尝试重新格式化您帖子中的代码以帮助我们帮助您。