简体   繁体   中英

Excel - Wrap range of rows at a given column, with first column repeating for the range of rows

Technically, the solution I'm looking for is to minimize actual programing. I need to know if there is Excel functionality I can invoke from VBA, instead of having to code the entire logic myself.

Imagine the following sheet:

Position   Competency1 Competency2 Competency3 Competency4
Employee1      x                                    x 
Employee2      x                        x

What I need is the following result:

Position   Competency1 Competency2
Employee1      x                  
Employee2      x                  

Position   Competency3 Competency4
Employee1                   x                  
Employee2      x                  

In other words, I want Excel to move the columns down, but maintain the first column. Now for the complicated bit; there will be several positions, with various employees. So a final product might look like:

Position   Competency1 Competency2
Employee1      x                  
Employee2      x                  

Position   Competency3 Competency4
Employee1                   x                  
Employee2      x                  

Position2  Competency5 
Employee3      x                              
Employee4              

The data is fetched from a database and formatted into the above layout via VBA. The amount of positions and employees/competencies per position are all variable.

I can of course code the entire thing, but it would make my life a lot easier if I could just throw some parameters at Excel and have it do it for me. Problem is, what relevant functionality I've found appears to be sheet-wide, while I need to be able to specify multiple sets of rows and columns.

I rather suspect the answer is: "Suck it up and code it". But I don't know enough Excel to say for sure.

I'm assuming the first list will be Position with Employees down the bottom. And that no other lists are there. Assuming this: here is logically what you need to code

  1. numberOfColumns: Count the cells going down where the cell is not blank in column A (the position column) minus 1. This will give the number of employees (excluding the Position cell).

  2. numberOfRows: Count the number of cells going across (row 1). This will be the number of Competency cells minus 1 (excluding the Position cell).

  3. competancyColumns : Define a variable capturing how many competency columns they need for each position. This can be through a textbox or in a cell on another sheet or something eg a value of 2

  4. Start at A1 + competancyColumns. Copy and cut cells after to end of row.

  5. Start at A1 + numberOfRows + 1. Add another counter to keep track of position (counter) so after point 4 the counter should read 2 (0 + competancyColumns). Paste cells (from point 4)

Repeat process until the counter = numberOfcolumns. Obviously not starting at A1 but the row you were at.

You can store it in an Array if you don't want to use Cut and Paste, but thats for you to decide.

SO that should be the basic logic.

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