[英]VBA to copy data into Columns
我目前有这个代码
Sheets("Pivot_Table_Non_Closed_Area").Range("E7:L7").Copy
'Pastes the data from the sheet above in the next avaliable row.
Sheets("Tracking_Table_Non_Closed_Area").Cells(Rows.Count, "C").End(xlUp).Offset(1). _
PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheets("Tracking_Table_Non_Closed_Area").Select
n = Cells(Rows.Count, "C").End(xlUp).Row
Range("A" & n) = Date
Range("B" & n) = Time
这是我当前代码的显示方式: https : //www.dropbox.com/s/p99kh0y3x2vsbo2/Currently_Presents.JPG?dl=0
但我似乎无法弄清楚如何将其从复制数据行和粘贴行更改为从数据列复制并粘贴到列中
这就是我希望新代码呈现数据的方式: https : //www.dropbox.com/s/krkdjlculdqpckn/Wish_for_it_to_Be_Presented.JPG?dl=0
希望这有意义
编辑:这是我当前的代码现在在所有帮助后的样子,但是仍然在日期和时间上挣扎
Sheets("Pivot_Table_002").Range("B10:B19").Copy
Sheets("Sheet1").Cells(7, Columns.Count).End(xlToLeft).Offset(0, 1). _
PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
n = Cells(7, Columns.Count).End(xlToLeft).Column
Range("A" & n) = Date
Range("B" & n) = Time
谢谢
编辑- Date
和Time
续
您正在使用Range
设置Date
和Time
的目的地,但是现在n
代表最后占用的列,您需要更改该逻辑。 让我们使用Cells
构造,在这种情况下,我认为它读起来更好:
Sheets("Tracking_Table_Non_Closed_Area").Cells(7, n) = Date
Sheets("Tracking_Table_Non_Closed_Area").Cells(8, n) = Time
.Cells
的工作方式如下:
.Cells(row_identifier, column_identifier)
有了这些,您应该已经准备就绪!
编辑- Date
和Time
让我们对Date
和Time
与对列式数据相同的策略。 原始设计执行以下操作:
n = Cells(Rows.Count, "C").End(xlUp).Row
那里到底发生了什么? n
是一个数字。 具体地说, n
是列“ C”中最后一个占用单元的行号。 我们感兴趣的是得到一排的最后占领柱代替-比方说,坚持使用下面的例子,我们在7行中的最后一栏占据:
n = Cells(7, Columns.Count).End(xlToLeft).Column
繁荣! 现在n
保留了最后占用的列号,您可以应用最后两行中的相同策略,根据提供的屏幕快照在Date
和Time
写入。
初步答案:
我认为剖析您已经存在的代码将对您有所帮助,所以让我们开始吧!
复制/粘贴操作发生在以下两行:
'This line does the copying
Sheets("Pivot_Table_Non_Closed_Area").Range("E7:L7").Copy
'This line does the pasting
Sheets("Tracking_Table_Non_Closed_Area").Cells(Rows.Count, "C").End(xlUp).Offset(1). _
PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
(为了清楚起见,我在缩进行上添加了缩进,因为_
是多行指示器。)
让我们来谈谈副本:
Sheets("Pivot_Table_Non_Closed_Area") '<~ this specifies the worksheet
Range("E7:L7") '<~ this specifies the range, which is a row-ish
' group of cells from E7 to L7
Copy '<~ this is the copy method
因此,如果您想使用一组列式单元格,则可以调整Range
。 为了举例说明,假设您对从E7到E11的5个单元格的列式组感兴趣。 如果要复制该组,请输入:
Sheets("Pivot_Table_Non_Closed_Area").Range("E7:E11").Copy
真好! 现在,让我们深入研究粘贴:
Sheets("Tracking_Table_Non_Closed_Area") '<~ this specifies the worksheet
Cells(Rows.Count, "C").End(xlUp).Offset(1) '<~ this starts in the last cell in
' column C (Rows.Count = the count
' of all the rows, i.e. 1 million-
' ish in Excel 2007+ or 56K-ish in
' Excel 2003). Then, .End(xlUp)
' simulates hitting Ctrl + Up on
' the keyboard, bringing you to the
' last occupied cell in column C.
' Finally, .Offset(1) increments
' that location by 1 row, bringing
' you to the cell immediately below
' the last occupied cell in
' column C.
PasteSpecial Paste:=xlPasteValues (then options) '<~ this does the pasting, with
' values-only (along with some
' other options, which aren't that
' important here.
酷吧? 找到最后一个被占用的行并在其下面立即写信息是VBA的基石,因此,我建议阅读该主题的杀手kill 。 那么,如果您想粘贴我们复制的列式区域,该区域在第7行中最后一个被占用列的右边一列上方? 我们可以这样写:
Sheets("Tracking_Table_Non_Closed_Area").Cells(7, Columns.Count).End(xlToLeft).Offset(0, 1). _
PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.