VBA Excel从单元格向左复制名称

尊敬的Stackoverflow用户,

我做了一些代码,只要这个单元格是空白的,就需要将单元格的名称复制到左边。

问题是,我只想要在第1行上运行代码。当我执行代码时,它会遍历整个工作表。

我怎样才能修改它,所以它只执行行1的行动。

Sub Test() Dim LastCol As Integer Dim WS As ActiveSheet With WS LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column End With For i = 2 To LastCol If WS.Cells(1, i) = Empty Then WS.Cells(1, i).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=RC[-1]" End If Next i End Sub 

提前致谢!

如果第一行中的任何单元格为空,则下面的代码将从单元格的文本复制到最后一列。

 Sub Test() Dim LastCol As Integer Dim WS As Worksheet Set WS = ThisWorkbook.Sheets("Sheet3") 'change Sheet3 to your data sheet With WS LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column For i = 2 To LastCol If IsEmpty(.Cells(1, i)) Then .Cells(1, i).Value = .Cells(1, i).Offset(0, -1).Value End If Next i End With End Sub