vba 在最后一行下方添加行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22045353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add rows below last row
提问by Reindeer
I'm designing a data submission spreadsheet with 12 rows of data (currently row 22 to 33). Rows 34 to 42 are filled with text. I'd like to add a new row (row 34) below row 33 if more than 12 data sets are to be entered, with the same format as row 33, using a macro assigned to a button. That's easy, but if I need another new row (now row 35) below row 34, I need a code is flexible to do that; mine always adds the new line below row 33, not below the last row of the data entry set. How do I do this? I'm quite new to VBA...
我正在设计一个包含 12 行数据(当前为第 22 到 33 行)的数据提交电子表格。第 34 到 42 行填充有文本。如果要输入超过 12 个数据集,我想在第 33 行下方添加一个新行(第 34 行),格式与第 33 行相同,使用分配给按钮的宏。这很容易,但是如果我需要第 34 行下方的另一个新行(现在是第 35 行),我需要一个灵活的代码来做到这一点;我的总是在第 33 行下方添加新行,而不是在数据条目集的最后一行下方。我该怎么做呢?我对VBA很陌生...
Thanks,
谢谢,
Andy
安迪
采纳答案by 2tsai
I don't think Don's answer will work, because you state you have text in rows 34 to 42.
我不认为唐的回答会奏效,因为你说你在第 34 到 42 行有文字。
If the text value of the cell in row 34 doesn't change, you can search for that cell and insert a row above it.
如果第 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
回答by Reindeer
lastrow = Cells(Rows.Count, "A").End(xlUp).Row + 1
Change "A" to the name of a column that always has data in it.
将“A”更改为始终包含数据的列的名称。
your lastrow variable can then be used as:
然后您的 lastrow 变量可以用作:
Range("A" & lastrow).Value = "blah"