[英]Copying and pasting info from one sheet to another using lastcol & lastrow
I understand copying and pasting is not a recommended way to go, however am proceeding with it. 我知道复制和粘贴不是推荐的方式,但是正在继续进行。 I currently have a amount of data on each sheet.
我目前每张纸上都有大量数据。 I am avoiding the final row when I copy the data from the rows(Works perfect).
从行复制数据时,我避免使用最后一行(完美)。 And I am avoiding the final column when I copy the columns(Works perfect).
而且我在复制列时避免使用最后一列(完美)。
LastRow: LastRow:
Lastrow = Cells(Rows.count, "A").End(xlUp).Row - 1
LastColumn: 最后一列:
With ActiveSheet
lastcol = .Cells(4, .Columns.count).End(xlToLeft).Column - 1
End With
I however need to now copy the data. 但是,我现在需要复制数据。 I can copy the rows perfectly fine.
我可以完全复制行。 However the columns is where I am getting caught up on.
但是,专栏是我紧追不舍的地方。 I'm quiet new to the Copy/Paste method, and am unsure how to go along of fixing this as I always used a range of cell names.
我对复制/粘贴方法很陌生,并且由于我一直使用一系列单元格名称,因此不确定如何解决此问题。
This will give me the rows needed, and all the columns needed as I am copying past the last column. 这将为我提供所需的行,以及在我经过最后一列进行复制时所需的所有列。
Sheets("Sheet2").Range("A4:AZ" & Lastrow).Copy Sheets(PlantArr(e)).Range("A1") 'Copy info to new sheet
How can I convert this so that AZ is actually the last column needed? 我如何转换它,以便AZ实际上是所需的最后一列? Is there an easy way to go about this or will I need to set up a case to convert column number to cell value?
有没有简单的方法可以解决此问题,还是需要设置一个案例将列号转换为单元格值?
Answer: 回答:
gAddress = Split(Cells(1, lastcol).Address(True, False), "$") 'Converts Lastcol number into alphabet character
letter = gAddress(0)
Sheets("Sheet2").Range("A4:" & letter & Lastrow).Copy Sheets(PlantArr(e)).Range("A1") 'Copy info to new sheet
After a bit of digging around, I figured out that you can pull an address from a column value. 经过一番挖掘,我发现您可以从列值中提取地址。 I hope the code below helps anyone who would like to convert a column number to an alphabet column.
我希望下面的代码对希望将列号转换为字母列的人有所帮助。
Dim gAddress
gAddress = Split(Cells(1, lastcol).Address(True, False), "$")
letter = gAddress(0)
It's possible to specify the copy Range by explicitly stating which cells compose that range. 通过明确说明组成该范围的单元格,可以指定复制范围。 Using 'With', you can write something like this to accomplish what you need:
使用“ With”,您可以编写如下代码来完成所需的操作:
Sub copy_col_and_rows()
Dim Lastrow As Integer: Dim lastcol As Integer:
With ActiveSheet
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row - 1
lastcol = .Cells(4, Columns.Count).End(xlToLeft).Column - 1
End With
With Sheets("Sheet2")
.Range(.Cells(1, 4), .Cells(Lastrow, lastcol)).Copy Destination:=Sheets(PlantArr(e)).Range("A1")
'Where .Cells(1,4) corresponds to "A4"
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.