0

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

Community
  • 1
  • 1
4d554d424c4553
  • 95
  • 2
  • 14
  • Based on this update, it looks like you're correctly identifying the column (`n`), but you're still forming your `Range` objects the same way you were before -- you need to modify those definitions as `n` no longer reflects the original quantity – Dan Wagner Jan 14 '15 at 15:55

1 Answers1

0

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!

Community
  • 1
  • 1
Dan Wagner
  • 2,693
  • 2
  • 13
  • 18
  • Thanks @Dan for this, the only bit I am now struggling with is the last bit of code with the Date and time following the pasted columns – 4d554d424c4553 Jan 08 '15 at 15:22
  • What is actually happening at the end there? It might be helpful to include a screenshot or description of what the final `Range` assignments are supposed to be accomplishing – Dan Wagner Jan 08 '15 at 15:41
  • I have added links to screenshots in the original question (didn't have enough points to add screenshots in). Links: How my current code presents the data: https://www.dropbox.com/s/p99kh0y3x2vsbo2/Currently_Presents.JPG?dl=0 How I want it to now present the data: https://www.dropbox.com/s/krkdjlculdqpckn/Wish_for_it_to_Be_Presented.JPG?dl=0 So the code you have given me works a treat for place the data in columns I just need the date and time to follow the data as shown in the screenshots – 4d554d424c4553 Jan 09 '15 at 07:16
  • I am afraid not, I have taken your code suggestions and done this: 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 (http://tinyurl.com/ka9pbyq - True structure) The problem is the Date and time are going down in rows and not across following the columns of data e.g 2nd screenshot (http://tinyurl.com/mqemk7n) – 4d554d424c4553 Jan 14 '15 at 07:33
  • Reading code from inside the comment is really difficult -- can you add it to your original question instead? – Dan Wagner Jan 14 '15 at 12:53
  • I have added it to the bottom of the original question. Thanks – 4d554d424c4553 Jan 14 '15 at 14:00