A行中每个可见单元格的Excel vba,在H行中插入值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28609977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 06:17:06  来源:igfitidea点击:

Excel vba for each visible cell in A row, insert value in H row ?

excelvbaexcel-vba

提问by Sabelo Ngcobo

How do I insert value in the H row based on the visibility status of A row in excel using vba?

如何使用vba根据excel中A行的可见性状态在H行中插入值?

Dim rng As Range 
    Set rng = Range("A2", Range("A65536").End(xlUp)).SpecialCells(xlCellTypeVisible) 
    For Each cell In rng 
         rng.value = "Something"
    Next cell 

The code above will populate 'something' to A row, but I don't know how to map this to H row (where) I want it ? Anyone have any ideas...

上面的代码会将“某物”填充到 A 行,但我不知道如何将其映射到我想要的 H 行(哪里)?谁有想法...

回答by chancea

You can use the Rowproperty of each cellin your iteration to map to the correct row in column H

您可以在迭代中使用Roweach的属性cell映射到 H 列中的正确行

Sub Macro1()
Dim rng As Range
    Set rng = Range("A2", Range("A65536").End(xlUp)).SpecialCells(xlCellTypeVisible)
    For Each cell In rng
        Range("H" & cell.Row).Value = "Something"
    Next cell
End Sub

回答by Gary's Student

If its the same value for each row, you do not need a loop:

如果每行的值相同,则不需要循环:

Sub NoLoop()
    Dim r As Range
    Set r = Range("H2:H" & Cells(Rows.Count, "H").End(xlUp).Row)
    Set r = r.Cells.SpecialCells(xlCellTypeVisible)
    r.Value = "whatever"
End Sub