简体   繁体   中英

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

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

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

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. Let's use the Cells construct, which I think reads better in this case:

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(row_identifier, column_identifier)

With that, you should be all set!

Edit - the Date and Time

Let's apply the same strategy to the Date and Time that we did to the column-ish data. The original design does the following:

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

What's actually happening there? n is a number. Specifically, n is the row number of the last-occupied cell in column "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:

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.

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 . For the sake of an example, let's say you're interested in the column-ish group of 5 cells from E7 to E11. 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 . 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? 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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