简体   繁体   English

VBA Excel范围到单元格

[英]VBA Excel Range to cell

I need to copy a range of cells into another range indicating the first cells. 我需要将一个单元格区域复制到另一个表示第一个单元格的区域。 Something like that: 像这样:

.Cells(1,1) = .Range("B1:B5")

Some help please, it doesn't work and I'm completely new in VBA Excel! 请提供一些帮助,它不起作用,并且我是VBA Excel的全新用户!

I'm not sure what exactly you're trying to copy or to where, but... 我不确定您要复制的内容到底是什么还是要复制到什么地方,但是...

With Workbooks("Book1").Worksheets("Sheet1")
    .Cells(1, 1).Value = .Range("B1").value
End With

will copy the contents of Cell B1 to Cell A1 on the same page. 会将单元格B1的内容复制到同一页上的单元格A1中。 If you need to copy the 5 cells in the Range B1:B5 you need to specify a range as your first argument. 如果需要复制范围B1:B5中的5个单元格,则需要指定一个范围作为第一个参数。 You can do so as such: 您可以这样做:

.Range(.Cells(1, 1).Address & ":" & .Cells(5, 1).Address).value = .Range("B1:B5").value

Which will copy the contents of B1 to B5 over to A1 to A5. 这会将B1到B5的内容复制到A1到A5。

Excel treats any number of cells inside a workbook be it 1 or 20 or more as a range, Cells lets you reference a single cell only. Excel会将工作簿中的任意数量的单元格视为1或20或更多作为范围,单元格使您只能引用单个单元格。 When you need to specify a range like Range("B1:B5") you can find the cells that define it and use .Address to get their string formatted range address. 当您需要指定一个范围(例如Range("B1:B5") ,可以找到定义该范围的单元格,并使用.Address来获取其字符串格式的范围地址。 So Cells(1, 1).Address returns "$A$1" and conatenating these in a range reference will get you a range so long as you remember the ":" in the middle. 因此, Cells(1, 1).Address返回"$A$1"并在范围引用中并置它们将为您提供范围,只要您记住中间的“:”即可。

Hope that helps. 希望能有所帮助。

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

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