I have worked on this problem for my entire day and can’t solve it.
The input data consists of several data blocks with the same number of rows and columns. Each data block has its name in the first line within the block. Besides, they are further separated by a blank row.
block1
name score value
a 2 3
b 3 5
c 1 6
block2
name score value
a 4 6
b 7 8
c 2 6
block3
name score value
a 5 4
b 7 8
c 2 9
The desired output is to extract the name and value column of each block, and then parallel them in columns. Like this:
value block1 block2 block3
a 3 6 4
b 5 8 8
c 6 6 9
Thanks for your help!
UPDATE
Thanks for your answer, Tony, and others!
I just have another requirement. It is possible that some row in some tables are missing. In other words, as you mentioned previously, the row number may vary. Is it possible to fill in the corresponding cell in these tables with NA? i.e. the new input is like:
block1
name score value
a 2 3
c 1 6
block2
name score value
a 4 6
b 7 8
c 2 6
block3
name score value
a 5 4
b 7 8
The desired output now is like this:
value block1 block2 block3
a 3 6 4
b NA 8 8
c 6 6 NA
UPDATE on Jul.3 (If it’s inappropriate to make the question too long, I will move this part and make it a new question)
block1
name score value
a 2 3
b 3 5
c 1 6
block2
name score value
a 4 6
b 7 8
c 2 6
block3
name score value
a 5 4
b 7 8
c 2 9
How can I pull both the value and its corresponding score and put them into one cell? Like this: The code indicates that the value is put into an dynamic array. Then the .range is assigned to this array. My first thought is to construct another array to store the value of the “score” column. Then loop through each element in both array, and concatenate them together. However, it seems that VBA does allow me to loop through the array, since its dimension is not defined. I tried REDIM, but it did not work.
value block1 block2 block3
a 3(2) 6(4) 4(5)
b 5(3) 8(7) 8(7)
c 6(1) 6(2) 9(2)
First answer – introduction to issues and request for clarification
This is not a solution – you do not give enough information for a solution – but introduces the issues and possible techniques. Warning: I have typed this into NotePad; no guarantees that there are no syntax errors.
You say each table is the same size although I assume not 3×3. But if they were 3×3, could I say table 1 starts in row 1, table 2 starts in row 7 and table N starts in 6(N-1)+1? That is, can you calculate the position of each table or do you need to search?
If you need to search, the following might help:
ColSrcLast = .Cells(RowCrnt,Columns.Count).End(xlToLeft).Columnis the VBA equivalent of placing the cursor in the last column of row RowCrnt+1 and then clicking Control+Left. This is probably the easiest way of finding the last used column in table 1.Control+ArrowKey moves the cursor in the indicated direction and:
Experiment and the above will become clearer.
If the number of blank lines between tables might vary, I think the following will be the easiest method of locating each table:
Within the above loop we have: Process table RowSrcTableTitle to RowSrcTableLast.
Is the Name column always column “A”? Is the Value column always the last column? If not, you will have to search across the header row for the column names.
Is every table in the same sequence? If not, you will have to sort them. Does every table contain every row? If not, your code for combining the tables will have to allow for this.
I hope the above gets you started. Come back if you have specific questions.
Second answer – Response to clarification
I created a test worksheet Jia Source which looks like this:
You say that the tables are all the same size. In this situation, the following code outputs to the Immediate Window the dimensions of each table. The output from this code is:
For your tables you will need to change the values of constants TableHeight and TableWidth. You will also have to change “Jia Source” to the name of your source worksheet.
I have left the code that shows how to search for the tables if they vary in size. If the above code does not produce the correct results for your worksheet, you may need to merge the two routines.
The following assumes RowSrcTitle, RowSrcHeader, RowSrcLast, ColSrcLeft and ColSrcRight are correct. It is the code from ExtractValue() plus the code to copy the data to the destination sheet which I have named “Jia Destination”. Its output is:
Have a play. Come back with questions if necessary.