我是VBA Excel的新手,想知道您能否提供帮助。
我尝试输入与此=IF(C10,(ROW(A10)-ROW(A$9)),"")
类似的内容,并且可以正常工作。但是,这样做的缺点是您必须在要自动填充的每个单元格中输入此公式。我试图在Excel中找到宏代码,以便单元格将自动编号1,2,3,等等。每当相邻列包含数据时?
例如,当用户将数据输入B1
时,A1
将自动填充为1。然后,当用户将数据输入B2
时,A2
将自动变为填充到2,依此类推。然后,当用户从B列删除数据时,A中的相邻列将不包含数字。
答案 0 :(得分:1)
根据您所说的内容输入A1
:
=if(isblank(B1),"",row())
如果要在VBA中使用此功能,则可以使用以下内容,但前提是您的数据从第1行开始。如果要使第2行的编号从1开始,只需在RngA部分后面添加“ -1”即可。
Sub Serial()
Dim RngA As Long, LastRow as Long 'Declares the variables used
LastRow = ActiveSheet.Cells(Cells.Rows.Count, "B").End(xlUp).Row 'This finds the last row in Column B where the loop will end.
For RngA = 1 To LastRow ' This loops from the first row to the last cell filled in column B
If Cells(RngA, 2) <> "" Then ' If Column B is blank, skip this row
Cells(RngA, 1).Value = RngA 'Column B was not blank, so put the row number in column A
End If
Next
End Sub