简体   繁体   English

使用lastcol和lastrow将信息从一张纸复制并粘贴到另一张纸

[英]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.

相关问题 VBA使用lastrow范围将数据从一个工作簿复制到另一个工作簿 - VBA Copying data from one workbook to another using lastrow ranges 复制并粘贴到另一个工作表中 - Copying and pasting in another sheet 从一张粘贴复制到另一张而不使用剪贴板的情况下,始终需要粘贴到第一行空白。出现错误424 - Copying from one sheet pasting to another sheet without using the clipboard, always needs to paste in the first empty row.Getting error 424 从一个工作表复制数据,然后将其粘贴到另一个Excel工作表时,Openpyxl边界线中断 - Openpyxl border line breaks when copying data from one sheet and then pasting it to another excel sheet 根据匹配将行信息从一张纸复制到另一张纸 - Copying Row Info from one sheet to another based on match 根据单元格输入将行信息从一个工作表复制到另一个工作表 - Copying row info from one sheet to another, based on cell input 从一张纸复制数据并将其粘贴到第二张纸的错误位置 - Copying data from one sheet & pasting it in the wrong location on the second sheet 从另一张纸复制一个公式并将其粘贴到主纸 - copying a formula from another sheet and pasting it to master sheet 单击命令按钮时,将相同范围的数据从一张纸复制并粘贴到另一张纸上 - On clicking Command Button, copying and pasting data of same range from one sheet to another 从一张纸复制范围并粘贴为另一张图像,同时调整图像大小 - Copying range from one sheet and pasting as image in another, all while resizing the image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM