Cách gói dữ liệu thành nhiều cột trong Excel - Thủ thuật Excel

Mục lục

Gwynne có 15 nghìn hàng dữ liệu trong ba cột. Cô ấy muốn in dữ liệu với 6 cột mỗi trang. Ví dụ: 50 tên đầu tiên trong A2: C51, sau đó là 50 tên tiếp theo trong E2: G51. Sau đó, di chuyển 50 hàng thứ ba thành A52: C101, v.v.

Thay vì giải quyết vấn đề này bằng các công thức, tôi sẽ sử dụng một chút Excel VBA để sắp xếp lại dữ liệu.

Macro VBA sẽ để dữ liệu ở dạng A: C. Một cột trống sẽ xuất hiện trong D. Dữ liệu mới sẽ xuất hiện trong D: F, cột trống trong G, dữ liệu mới trong H: J.

Ghi chú

Gần 10 năm trước, tôi đã trả lời một câu hỏi làm thế nào để gắn 1 cột thành 6 cột. Trong trường hợp này, dữ liệu được sắp xếp theo chiều ngang, với Apple ở C1, Banana ở D1, Cherry ở E1,… Hình trong H1, sau đó là Guava bắt đầu ở C2, v.v. Hồi đó, tôi trả lời câu hỏi bằng công thức. Bạn có thể xem video cũ đó: tại đây.

Bước đầu tiên là tìm ra bao nhiêu hàng vừa với trang in của bạn. Đừng bỏ qua bước này. Trước khi bắt đầu với macro, bạn cần thực hiện tất cả những điều sau:

  • Đặt lề trên tab Bố cục trang của ruy-băng
  • Nếu bạn muốn các tiêu đề của mình từ Hàng 1 lặp lại trên mỗi trang, hãy sử dụng Bố cục Trang, Các hàng lặp lại ở trên cùng và chỉ định 1: 1
  • Chỉ định bất kỳ đầu trang và chân trang nào sẽ xuất hiện trên mỗi trang.
  • Sao chép các tiêu đề từ A1: C1 qua E1: G1.
  • Sao chép các tiêu đề từ A1: C1 sang I1: K1.
  • Chỉ định E: K làm phạm vi in
  • Điền các số từ 1 đến 100 trong E2: E101 với =ROW()-1
Thiết lập trang sẽ in.

Khi tất cả các cài đặt trang của bạn đều chính xác, hãy sử dụng Ctrl + P để hiển thị tài liệu Xem trước khi in. Nếu cần, hãy nhấp vào ô Hiển thị Xem trước Bản in ở giữa màn hình. Trong Bản xem trước khi in, hãy tìm số hàng cuối cùng trên trang 1. Trong trường hợp của tôi, nó là 46. Đây sẽ là một số quan trọng trong tương lai.

Hiển thị tài liệu Xem trước khi In.

Để tạo macro, hãy làm theo các bước sau:

  1. Lưu sổ làm việc của bạn dưới dạng tên mới trong trường hợp có sự cố. Ví dụ: MyWorkbookTestCopy.xlsx
  2. Nhấn alt = "" + F11 để mở VBA Editor
  3. Từ menu VBA, chọn Chèn, Mô-đun
  4. Sao chép mã sau và dán vào cửa sổ mã

    Sub WrapThem() ' the following line says XLUP not x1up ! FinalRow = Cells(Rows.Count, 1).End(xlUp).Row ' Change 46 to match your Rows Per Page RowsPerPage = 46 NextRow = 2 NextCol = 5 For i = 2 To FinalRow Step RowsPerPage Cells(NextRow, NextCol).Resize(RowsPerPage, 3).Value = _ Cells(i, 1).Resize(RowsPerPage, 3).Value If NextCol = 5 Then NextCol = 9 Else NextCol = 5 NextRow = NextRow + RowsPerPage End If Next i End Sub
  5. Tìm dòng có nội dung RowsPerPage = 46và thay thế số 46 bằng số hàng mà bạn tìm thấy trong Bản xem trước khi in của mình.

Dưới đây là một số điều khác mà bạn có thể phải thay đổi tùy thuộc vào dữ liệu của mình:

Các FinalRow =hình dòng cho mục cuối cùng trong cột 1. Nếu dữ liệu của bạn bắt đầu trong cột C thay vì cột A, bạn sẽ thay đổi điều này:

FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

cái này

FinalRow = Cells(Rows.Count, 3).End(xlUp).Row

Trong ví dụ này, vị trí đầu tiên cho dữ liệu mới sẽ là ô E2. Đây là hàng 2, cột 5. Nếu bạn có năm dòng tiêu đề và dữ liệu mới của bạn sẽ bắt đầu trong G6, bạn sẽ thay đổi NextRow = 2thành NextRow = 6. Đổi NextCol = 5thành NextCol = 7(vì cột G là cột thứ 7).

Trong ví dụ này, dữ liệu bắt đầu bằng A2 (ngay sau tiêu đề trong hàng 1). Nếu bạn có 3 dòng tiêu đề, dữ liệu của bạn sẽ bắt đầu bằng A4. Thay đổi dòng này:

For i = 2 To FinalRow Step RowsPerPage

đến dòng này:

For i = 4 To FinalRow Step RowsPerPage

Các cột đầu ra của tôi xuất hiện trong cột E (cột thứ 5) và cột I (cột thứ 9). Giả sử bạn có bốn cột dữ liệu. Dữ liệu ban đầu là B: E. Đặt tập hợp cột đầu tiên trong G: J và L: O. G là cột thứ 7. L là cột thứ 12. Trong văn bản sau, hãy thay đổi 3 thành 4 ở hai nơi vì bạn có 4 cột thay vì 3. Thay đổi 5 thành 7 ở hai nơi vì cột đầu ra đầu tiên là G thay vì E. Thay đổi 9 thành 12 vì cột đầu ra thứ hai là L thay vì tôi.

Thay đổi điều này:

Cells(NextRow, NextCol).Resize(RowsPerPage, 3).Value = _ Cells(i, 1).Resize(RowsPerPage, 3).Value If NextCol = 5 Then NextCol = 9 Else NextCol = 5 NextRow = NextRow + RowsPerPage End If

đến điều này:

Cells(NextRow, NextCol).Resize(RowsPerPage, 4).Value = _ Cells(i, 1).Resize(RowsPerPage, 4).Value If NextCol = 7 Then NextCol = 12 Else NextCol = 7 NextRow = NextRow + RowsPerPage End If

Bây giờ bạn đã sẵn sàng để chạy macro. Lưu sổ làm việc lần cuối.

Trong cửa sổ VBA, bấm vào bất kỳ đâu bên trong macro. Trong hình bên dưới, con trỏ ở ngay sau Sub WrapThem(). Nhấn phím F5 hoặc nhấn vào biểu tượng Run như hình bên dưới.

Chạy macro trong VBA.

Chuyển về Excel. Bạn sẽ thấy kết quả như thế này:

Xem kết quả trong Excel.

Đảm bảo rằng họ trên trang 1 cột E được theo sau đúng với tên trong trang 1 cột I.

Xác minh kết quả.

Xem video

Các bước này được giải thích trong video này:

Bản ghi video

Học Excel cho Podcast, Tập 2194: Gói các Cột.

Chào mừng bạn trở lại netcast, tôi là Bill Jelen. Câu hỏi hôm nay, do Gwen gửi đến. Gwen đang xem video 984, được gọi là Cột quay lén. Đây là từ nhiều năm trước, và tôi thực sự đã sử dụng một công thức để giải nó hồi đó, nhưng vấn đề về cặp song sinh này phức tạp hơn.

So she has a three column worksheet with around 15,000 rows. and needs to make each page six columns. So, on the first page, these 60 cells; and then next to it, the next 60 cells. Now, Gwen has figured out that she can fit about 60 rows. But for anyone else watching this, the most important part here is to figure out how many rows because you'll really screw things up if you make any of these changes after the fact.

Alright, so for me, what I'm going to do is I'm going to come here to page layout, I'm gonna declare that these seven columns are going to be my print area-- Print Area, Set Print Area. I'm going to go into Print Titles and say that “Rows to repeat at top” is 1:1. I'm going to go… Actually, I'd like to use Margins here-- Margins, Narrow, and then back in the Page Setup, Header/Footer, and choose whatever my, you know, Custom Footer should be-- Confidential. Do all of the those settings, anything you're ever going to change first. Alright? Because that's going to change the number of rows per page.

Now, I'm going to type in the number 1 here, this is just going to be some temporary data. I'm going to hold down the Ctrl key and grab the Fill handle, and go down until I'm sure I'm past the first page like that. And then, we'll just do a Print Preview-- Ctrl+P, Show Print Preview-- and you'll notice that I have 46 rows that fit on the first page. And let's just check, go to the second page-- so 46 plus 46 is 92, so we're getting 46 rows per page, 46 rows per page. That number is incredibly important-- 46. In fact, I'm going to write it down over here just so I don't forget-- 46 rows per page.

Alright, now, I'm going to solve this today with a Macro; back in video 984, I used some complex formulas to do it, but today a macro feels better. If you've never used macros before don't be intimidated. Here's how we start: We press Alt+F11-- Alt+F11-- that brings open this screen and actually, the very first time that you open Alt+F11, it's going to be just a big gray screen-- probably a lot like this-- like that. So you want to say, View, Project Explorer, Find your workbook here, and say Insert Module-- I've already done that-- and what we'll get-- and what we get-- is a white screen. And over here in this white screen, you're going to type this code, alright? The word "Sub" which means that this is a subroutine, and then any naming you want-- I call it WrapThem, no spaces there, so just jam everything together-- open and closing parenthesis. Then we're we're going to create a variable: FinalRow = Cells(Rows.Count, 1).End, and these four letters here are XL, not X1-- everybody screws this up, XL. And you can type it in all caps if you want but they're going to change it back to that format where the L looks like a 1-- don't put a 1 there. Rows.Per.Page-- and this is where you put whatever number you figured out. Now, for me it's 46; for Gwen, it sounds like it's 60. And then, the next row where we want the first data to go is Row 2, and then the next column-- 1, 2, 3, 4, 5-- is Column 5.

Alright, so I set this up. And then, the rest of this is going to be very, very generic. it's going to work with, you know, any size data set: For I (it's a variable) = 2 To FinalRow (that's how many rows we had) Step (that means every time through the loop we're going to increase by) RowsPerPage (which in this case is 46, for Gwen's case it's going be 60). We're going to say: Cells(NextRow, NextCol) -- so, next row's going to be 2, Column 5-- .Resize(RowsPerPage, 3) -- resize 46 rows, 3 columns-- .Value = _ (and that's an underscore there) It's going to be equal to Cells(1, 1) -- so whatever is in Row 2 comma 1, Column 1-- .Resize(RowsPerPage, 3).Value. And then, what we have to do is, we have to be a little bit clever here about after we paste the first 46 times 46 rows, by 3 columns.

Where do we go next? There, right? So, if currently, the next column is pointing to Column E, well, then I need the next one to go to Column I. I is the ninth column. Alright. So that's why we say NextCol = 5. But if we're not… NextCol = 5 that means our NextCol = 9. Then we're going to reset the next group back to Column E and the NextRow is going to be = whatever the previous row was, + 46. And then next time… now here, let's just walk through this, you don't have to run it one step at a time. But I'm going to do that with F8-- just to see what we get here.

And so, what we've learned, is the final row is real-- 15,582. We're about to write to row 2, column 5. And so: For I = 2 To FinalRow. The first time through, I is going to be equal to 2. We're going to say that Row 2, Column 5, is going to be equal to Row 2, Column 1-- 46 rows, 3 columns. I want to run this with F8. We'll look over here in the spreadsheet and we'll see that it turned out those first 46 came to this area. Alright. But, we're going to let this run again. Alright.

Now, the second time through the loop, the I has jumped up from 2 to 48. Alright. And so this time, we're going to be running to Row 2, Column 9, and we're going to be getting data from Row 48. Alright, now let's go check this one right here. So, what we see is Andy Hartley-- that works great-- down here at the end, Kelly Ferguson. But the next person should be Lue Rahman-- Rahman-- and that works, and it goes down to Lue Harvey, right there. Alright. Now, what we're hoping next time, is we get Barb Davison. I'll press F8 few more times, here's the next one and we look, and it's now writing to Row 48. Alright. And it's Barb Davison, and it appears to be working. At this point, I'm happy with it, I'm just going to click run.

And, actually, you don't have to go-- if you're not creating a video to explain this to somebody-- you don't have to go through and press F8; you could just come up here, click inside WrapThem, click run, and that fast it will take your data and wrap it into two columns.

Now, some things I see here-- Surname isn't wide enough, that should not affect our page layout, I'm hoping. And when I do Print Preview, I now have 170 pages. Data there, Page 2, Page 3, Page 4. Now, if we would change the margins at this point, everything is going to be screwed up-- it's going to be horrible. That's why it's really, really important, right up front, you have to do all of your page layout things before you calculate that 46. Now, of course, at this point, Save your workbook with a new name, alright? We don't want to destroy the personal workbook. And then you can delete columns A through D, and you have your results.

Now, if you want to learn about macros-- macros are incredibly powerful-- we probably could have solved this with a formula. And, certainly, the me from 10 years ago solved it with a formula, but at this point in my life, just a simple little 15 line macro is a lot easier. This book, by Tracy Syrstad and myself, will teach you all about macros.

Alright, wrap-up for this Episode: How to wrap 3 columns of data in 2 sets of columns per page. The super important step, you have to do all the page setup things first, Rows to Repeat at Top, Margins, Header/Footer, and then just type some numbers-- 1 through whatever-- I use the Fill handle with control; go to Print Preview, How many rows per page; switch over to Alt+F11; Insert a module and then type the code that I showed you in the video; click run. And most of the time, I advise people to save your workbook as xlsm, but in this case this was a one-time thing, I'm suspecting. So if you're, you know, just want to have that macro disappear, keep it as xlsx, save the file, it'll warn you that you're about to lose your macro. That's probably okay, because we've solved the problem well.

Này, tôi muốn cảm ơn Gwen vì đã gửi câu hỏi đó, tôi muốn cảm ơn bạn đã ghé qua. Chúng tôi sẽ gặp bạn lần sau cho một netcast khác từ.

thú vị bài viết...