Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 循环遍历单元格并添加到一个范围_Vba_Excel - Fatal编程技术网

Vba 循环遍历单元格并添加到一个范围

Vba 循环遍历单元格并添加到一个范围,vba,excel,Vba,Excel,如果单元格B1到J1满足特定条件,我将如何循环单元格B1到J1并将其添加到范围中。比如说 Dim Range1 As Range For i = 1 to 9 If Range("A1").Offset(1,i) meets a certain criteria Then **Add that cell to Range1** End If Next i 我不知道如何将某些单元格添加到Range1中 谢谢你的帮助 使用Union将您的产品系列粘合在一起 请注意,对于i=1到x的方法,对于每个循

如果单元格B1到J1满足特定条件,我将如何循环单元格B1到J1并将其添加到范围中。比如说

Dim Range1 As Range
For i = 1 to 9
If Range("A1").Offset(1,i) meets a certain criteria Then
**Add that cell to Range1**
End If
Next i
我不知道如何将某些单元格添加到Range1中


谢谢你的帮助

使用
Union
将您的产品系列粘合在一起

  • 请注意,对于i=1到x的方法,
    对于每个
    循环都比
  • 您可能能够立即使用来确定新范围(例如,任何空格、任何错误、任何公式等)


  • 当我不想在工作表中添加代码时,我在即时模式下使用此方法

    strX="": _
    For Each cllX in Range( ActiveCell, Cells( Cells.SpecialCells(xlCellTypeLastCell ).Row, ActiveCell.Column) ): _
    strX=strX & iif(cllX.text="","",iif(strX="","",",")& cllX.address): _
    Next: _
    Range(strX).Select
    
    虽然这是直观的,但它只适用于35到50个细胞。之后,VBA返回错误1004

    Run-time error '1004':
    Application-defined or object-defined error
    
    使用联合功能更为稳健

    Set rngX=ActiveCell: _
    For Each cllX in Range( ActiveCell, Cells( cells.SpecialCells(xlCellTypeLastCell ).Row, ActiveCell.Column) ): _
    Set rngX=iif( cllX.text="", rngX, Union(rngX, cllX) ): _
    Next: _
    rngX.Select
    

    它是如此的简短和直观,每次使用后我都会扔掉它。

    你确定“For each循环比For I=1 to x方法更快”吗?我认为反之,
    对于每个
    对于一个范围来说更快,
    对于i
    对于循环通过一个变量arrayo ok来说更快。thanx的澄清应该是“下一个C”还是无关紧要?
    Set rngX=ActiveCell: _
    For Each cllX in Range( ActiveCell, Cells( cells.SpecialCells(xlCellTypeLastCell ).Row, ActiveCell.Column) ): _
    Set rngX=iif( cllX.text="", rngX, Union(rngX, cllX) ): _
    Next: _
    rngX.Select