在最后一行下面添加行

时间:2014-02-26 15:06:33

标签: excel vba row addition

我正在设计一个包含12行数据的数据提交电子表格(目前第22行到第33行)。第34至42行填充文本。如果要输入的数据集超过12个,我想在第33行下面添加一个新行(第34行),使用与第33行相同的格式,使用分配给按钮的宏。这很容易,但是如果我需要在第34行下面的另一个新行(现在是第35行),我需要一个代码可以灵活地执行此操作;我总是在第33行下面添加新行,而不是在数据输入集的最后一行之下。我该怎么做呢?我对VBA很新......

谢谢,

安迪

2 个答案:

答案 0 :(得分:1)

lastrow = Cells(Rows.Count, "A").End(xlUp).Row + 1

将“A”更改为始终包含数据的列的名称。

您的lastrow变量可以用作:

Range("A" & lastrow).Value = "blah"

答案 1 :(得分:1)

我认为唐的回答不会起作用,因为你声明第34至42行有文字。

如果第34行中单元格的文本值没有改变,您可以搜索该单元格并在其上方插入一行。

Dim someText As String
someText = "Text value of row 34"

Dim currentRange As Range
Set currentRange = Selection

With Range("A:A").find(someText) 'change the range if needed
    .EntireRow.Insert
    .Offset(-2).EntireRow.Copy
    .Offset(-1).EntireRow.PasteSpecial xlPasteFormats 'change the paste type as needed
End With
Application.CutCopyMode = False 'get rid of the marching ants
currentRange.Select 'select whatever was selected before
'alternative: Range("A1").Select
相关问题