简体   繁体   English

VBA - 复制粘贴带有空白单元格的列

[英]VBA - Copy -pasting columns with blanks cells

Hi I am trying to write a code to copy values in different column from one sheet to another(Both sheet have same headers) There are few columns which I can simply copy and paste using the below code.嗨,我正在尝试编写代码以将不同列中的值从一张表复制到另一张表(两张表具有相同的标题)有几列我可以使用以下代码简单地复制和粘贴。 But for the column which have blank column in between what would be the best way to copy these column.但是对于中间有空白列的列,复制这些列的最佳方法是什么。 I know this can be done using variable , I am curious to find out if this can be done without using any variable.我知道这可以使用 variable 来完成,我很想知道这是否可以在不使用任何变量的情况下完成。

Also , is there any smarter way to write a code to copy all the columns in one go (with or without declaring any variables)另外,有没有更聪明的方法来编写代码以一次性复制所有列(无论是否声明任何变量) 在此处输入图片说明

Sheets("type 1").Select
Cells.Find(What:=" Netherland ").Select
ActiveCell.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("type 2").Select
Cells.Find(What:=" Netherland ").Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste 

You could try something like this你可以试试这样的

Worksheets("type 2").Columns(Worksheets("type 2").Find("*Netherland*").Column).Value _
= Worksheets("type 1").Columns(Worksheets("type 1").Find("*Netherland*").Column).Value

You could make this a lot cleaner by setting the column numbers to variables and just using them instead of the actual line to find them, but this doesn't require any additional variables.您可以通过将列号设置为变量并仅使用它们而不是实际行来查找它们来使这更清晰,但这不需要任何额外的变量。

You could try using the SpecialCells property to include the entire column, including any empty cells that appear between the first and last cells.您可以尝试使用SpecialCells属性来包含整个列,包括出现在第一个和最后一个单元格之间的任何空单元格。

So where you have:所以你有:

Range(Selection, Selection.End(xlDown)).Select

Instead use something like:而是使用类似的东西:

.Range("A1", Range("A1").SpecialCells(xlCellTypeLastCell))

Though you'll have to change the range references to suit your project.尽管您必须更改范围引用以适合您的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM