首页 > 解决方案 > VBA动态循环填充列

问题描述

我正在尝试用 1 从标题后的第二行到最后一行填充列的每一行。在名为 Adults 的工作表中,该列是“Ok to call”。此代码在我需要的正确工作表中创建列,但不会用 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

标签: vbaloopsdynamic

解决方案


在 PeterT 的建议之后添加了这个:

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

推荐阅读