简体   繁体   English

VBA将数据复制到列

[英]VBA to copy data into Columns

I currently have this code 我目前有这个代码

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

This is how my current code presents it: https://www.dropbox.com/s/p99kh0y3x2vsbo2/Currently_Presents.JPG?dl=0 这是我当前代码的显示方式: https : //www.dropbox.com/s/p99kh0y3x2vsbo2/Currently_Presents.JPG?dl=0

but I can not seem to work out how to change it from copying rows of data and pasting rows into copying from columns of data and pasting into columns 但我似乎无法弄清楚如何将其从复制数据行和粘贴行更改为从数据列复制并粘贴到列中

This is how I want the new code to present the data: https://www.dropbox.com/s/krkdjlculdqpckn/Wish_for_it_to_Be_Presented.JPG?dl=0 这就是我希望新代码呈现数据的方式: https : //www.dropbox.com/s/krkdjlculdqpckn/Wish_for_it_to_Be_Presented.JPG?dl=0

Hope this makes sense 希望这有意义

Edit: This is how my current code now looks after all the help, but stills struggling with the Date and time 编辑:这是我当前的代码现在在所有帮助后的样子,但是仍然在日期和时间上挣扎

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

Thanks 谢谢

Edit - the Date and Time continued 编辑- DateTime

You are setting the destination for Date and Time using a Range , but now that n represents the last-occupied column, you need to change that logic. 您正在使用Range设置DateTime的目的地,但是现在n代表最后占用的列,您需要更改该逻辑。 Let's use the Cells construct, which I think reads better in this case: 让我们使用Cells构造,在这种情况下,我认为它读起来更好:

Sheets("Tracking_Table_Non_Closed_Area").Cells(7, n) = Date Sheets("Tracking_Table_Non_Closed_Area").Cells(8, n) = Time Sheets("Tracking_Table_Non_Closed_Area").Cells(7, n) = Date Sheets("Tracking_Table_Non_Closed_Area").Cells(8, n) = Time

Here's how .Cells is doing the work: .Cells的工作方式如下:

.Cells(row_identifier, column_identifier)

With that, you should be all set! 有了这些,您应该已经准备就绪!

Edit - the Date and Time 编辑- DateTime

Let's apply the same strategy to the Date and Time that we did to the column-ish data. 让我们对DateTime与对列式数据相同的策略。 The original design does the following: 原始设计执行以下操作:

n = Cells(Rows.Count, "C").End(xlUp).Row

What's actually happening there? 那里到底发生了什么? n is a number. n是一个数字。 Specifically, n is the row number of the last-occupied cell in column "C". 具体地说, n是列“ C”中最后一个占用单元的行号。 We're interested in getting the last-occupied column in a row instead -- let's say, to stick with the example below, we the last-occupied column in row 7: 我们感兴趣的是得到一排的最后占领代替-比方说,坚持使用下面的例子,我们在7行中的最后一栏占据:

n = Cells(7, Columns.Count).End(xlToLeft).Column

Boom! 繁荣! Now that n holds the last-occupied column number, you can apply the same strategy that you have in your last two lines to write in the Date and Time per the screenshots you provided. 现在n保留了最后占用的列号,您可以应用最后两行中的相同策略,根据提供的屏幕快照在DateTime写入。

Initial Answer: 初步答案:

I think a dissection of your already-existing code will help you along here, so let's get after it! 我认为剖析您已经存在的代码将对您有所帮助,所以让我们开始吧!

The copy/paste action is happening on these two lines: 复制/粘贴操作发生在以下两行:

'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

(indentation added by me for clarity on the pasting line as _ is a multi-line indicator.) (为了清楚起见,我在缩进行上添加了缩进,因为_是多行指示器。)

Let's talk about the copy: 让我们来谈谈副本:

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

So, if you wanted to work with a column-ish group of cells instead, you'd adjust the Range . 因此,如果您想使用一组列式单元格,则可以调整Range For the sake of an example, let's say you're interested in the column-ish group of 5 cells from E7 to E11. 为了举例说明,假设您对从E7到E11的5个单元格的列式组感兴趣。 If you wanted to copy that group, you would write: 如果要复制该组,请输入:

Sheets("Pivot_Table_Non_Closed_Area").Range("E7:E11").Copy

Nice! 真好! Now let's dive into the paste: 现在,让我们深入研究粘贴:

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.

Cool, right? 酷吧? Finding the last occupied row and writing information immediately below it is a cornerstone of VBA, so I would recommend reading this killer writeup on that subject . 找到最后一个被占用的行并在其下面立即写信息是VBA的基石,因此,我建议阅读该主题的杀手kill So what if you wanted to paste the column-ish area we copied above one column right of the last occupied column in row 7? 那么,如果您想粘贴我们复制的列式区域,该区域在第7行中最后一个被占用列的右边一列上方? We could write this: 我们可以这样写:

Sheets("Tracking_Table_Non_Closed_Area").Cells(7, Columns.Count).End(xlToLeft).Offset(0, 1). _
            PasteSpecial Paste:=xlPasteValues, _
            Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Hope that helps! 希望有帮助!

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

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