Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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,我有一个工作表,从另一个工作表中导入一些行,并在其中添加一个列,该列包含每行的索引。此索引应递增(1、2、3等,直到导入最后一行)。 我希望它与宏“import”一起插入。 有人能帮我吗。工作表。我想,UsedRange是您要找的 Dim sh As Worksheet Dim y As Long Set sh = ActiveWorkbook.ActiveSheet ' or whichever For y = 1 To sh.UsedRange.Rows.Count '

我有一个工作表,从另一个工作表中导入一些行,并在其中添加一个列,该列包含每行的索引。此索引应递增(1、2、3等,直到导入最后一行)。 我希望它与宏“import”一起插入。
有人能帮我吗。

工作表。我想,UsedRange
是您要找的

Dim sh As Worksheet
Dim y As Long

Set sh = ActiveWorkbook.ActiveSheet     ' or whichever

For y = 1 To sh.UsedRange.Rows.Count
    ' Replace 5 by the column number you want the index in
    sh.Cells(y, 5) = y
Next y