【问题标题】:Excel VBA find the column in a Range("H2:FA2") with any cell that has a value of "2"Excel VBA 在 Range("H2:FA2") 中找到任何值为“2”的单元格的列
【发布时间】: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

我会尝试这里提到的其他答案,因为我想学习更快地完成工作的方法。

【问题讨论】:

  • 尝试重新格式化您帖子中的代码以帮助我们帮助您。

标签: excel vba


【解决方案1】:

尝试找到 2 并使用该列与您的行相交来设置零。

Sub findTwo()
    Dim c As Variant

    With Worksheets("sheet3")
        c = Application.Match(2, .Range("H2:FA2"), 0) + 7
        If Not IsError(c) Then
             Intersect(.Columns(c), Union(.Range("3:3,15:20,22:22,24:24,27:28,30:30,32:32"), _
                                          .Range("35:35,38:38,41:41,44:44,47:47,50:50,53:53"), _
                                          .Range("56:56,59:59,62:62,65:65,68:68,71:71,74:74"), _
                                          .Range("77:77,80:80,83:83,86:86"))) = 0
       End If
    End With
End Sub

【讨论】:

    【解决方案2】:

    您需要同时迭代行和列才能遍历整个范围。 您可以像通常在 Excel 中那样命名一个单元格,然后使用以下命令访问它:

    将 MyRange 调暗为范围

    设置 MyRange = Range("MyNamedRange")

    要迭代行和列:

    对于 MyRange 中的每一行

    对于行中的每个单元格

    '在单元格上做点什么...

    下一个单元格

    下一行

    如果您要处理大量数字,那么这种方法并不是那么好。如果您希望它更快,请使用 RangeToArray,反之亦然。欲了解更多信息 - http://www.cpearson.com/excel/ArraysAndRanges.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多