Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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_Loops_Dynamic - Fatal编程技术网

VBA动态循环以填充列

VBA动态循环以填充列,vba,loops,dynamic,Vba,Loops,Dynamic,我试图用1填充列的每一行,从标题后的第二行到最后一行。 在名为“成人”的工作表中,该列为“可以呼叫”。 这段代码在正确的工作表中创建我需要的列,但不使用1填充行。我想我必须在最后一行加上,但不知道怎么加。谢谢你的帮助 Dim shtA As Worksheet Set shtA = ActiveWorkbook.Sheets("Adults") Dim LastCol As Long LastCol = shtA.Cells(1, Columns.Count).End(xlT

我试图用1填充列的每一行,从标题后的第二行到最后一行。 在名为“成人”的工作表中,该列为“可以呼叫”。 这段代码在正确的工作表中创建我需要的列,但不使用1填充行。我想我必须在最后一行加上,但不知道怎么加。谢谢你的帮助

Dim shtA As Worksheet
Set shtA = ActiveWorkbook.Sheets("Adults")
Dim LastCol As Long
LastCol = shtA.Cells(1, Columns.Count).End(xlToLeft).Column
Dim lastRow As Long
lastRow = shtA.Cells(Rows.Count, 1).End(xlUp).Row 

'Loop through heading row to get column number of "Ok to call?"
Dim okcall As Long
Dim a As Long
For a = 1 To LastCol
If LCase(shtA.Cells(1, a).Value) = "ok to call?" Then
 okcall = a
    Exit For 
End If
Next a

在PeterT的建议后添加了以下内容:

For a = 1 + 1 To lastRow 
shtA.Cells(a, okcall).Value = 1 
Next a 
End

您想用什么填充这些行?您需要在这个循环之后添加第二个循环(您可以对循环变量再次使用
a
),使用
shtA.Cells(a,okcall).Value=
Yes!第二个循环成功了。我想要值“1”。这是我添加的:对于a=1+1到lastRow shtA.Cells(a,okcall).Value=1下一个a