Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reversing a Table 3

Status
Not open for further replies.

fanch72

IS-IT--Management
Joined
Mar 7, 2003
Messages
65
Location
SE
Hi!

I have the following table which I want to reorder:
A 1 2
B 3 4 5
C 5 6 7 8
D 1 3 5 7
E 2

The new table should look like this. Note that the numbers are not "numbers" but words.
1 2 3 4 5 6 7 8
A A B B B C C C
D E D C D
D


Can anyone help me with that?

Thanks a lot!
Francois
 
Hey fanch,
I played around a bit with this...dump the following code into a new module and assign it to a button. You'll have rename your sheet to "OldTable" or change the sheet name in the code for the oldtable variable to match your sheet name.

Also, change the values for firstrow, lastrow, firstcolumn, and lastcolumn to match your table size.

Hope it helps...

Dan



Sub transpose()
Dim oldtable As Worksheet
Dim newtable As Worksheet

'set this sheet name to whatever your sheet name is
Set oldtable = ThisWorkbook.Sheets("OldTable")

Dim firstrow, lastrow, firstcolumn, lastcolumn
Dim temp As Variant
Dim tempcellcontents() As Variant
Dim cellcontents()

Dim v As Integer
Dim w As Integer
Dim x As Integer
Dim y As Integer
Dim z As Integer
Dim present As Boolean
Dim cellcount As Integer
Dim rowheadings() As Variant

firstrow = 1 '1st row of data
firstcolumn = 2 '1st column of data (not the leftmost column)
lastrow = 5 'or wherever your table ends vertically
lastcolumn = 5 'or wherever your table ends horizontally

'determines how many cells in table
cellcount = ((lastcolumn + 1) - firstcolumn) * ((lastrow _
+ 1) - firstrow)

'load rowheadings for later use...
ReDim rowheadings(firstrow To lastrow)
For x = firstrow To lastrow
rowheadings(x) = Cells(x, firstcolumn - 1)
Next

'load array with cell contents
ReDim tempcellcontents(1 To cellcount)
ReDim cellcontents(1 To cellcount)

z = 0
For x = firstrow To lastrow
For y = firstcolumn To lastcolumn
z = z + 1
tempcellcontents(z) = Cells(x, y)
Next
Next

'pick out unique cells for new column headings

w = 1
For z = 1 To cellcount
temp = tempcellcontents(z) 'pick each cellcontents
'one at a time

present = False ' set initial present property
' to false...

For x = 1 To w 'compare it to all the others.
If temp = cellcontents(x) Then
present = True
End If
Next

If present = False Then 'add to new cell contents
cellcontents(w) = temp
w = w + 1
End If
Next

'create new table

ActiveWorkbook.Sheets.Add
ActiveSheet.Name = "NewTable"
Set newtable = ThisWorkbook.Sheets("NewTable")


'add columnheadings
For x = 1 To w
newtable.Cells(1, x) = cellcontents(x)
Next

'fill table
v = 2
For z = 1 To w
temp = cellcontents(z)
For x = firstrow To lastrow
For y = firstcolumn To lastcolumn
If oldtable.Cells(x, y) = temp Then
Do Until newtable.Cells(v, z) = ""
v = v + 1
Loop
newtable.Cells(v, z) = oldtable.Cells _
(x, firstcolumn - 1)
v = v + 1
End If
Next
v = 2
Next
Next

End Sub
 
Hi!
This really rocks! I am sorry I thought I was just missing one function in Excel, not a whole script! Thanks a lot for your help.

Francois
 
Sorry I wanted to give you a couple of stars but cant find where I should choose them!
 
Francois - just click on the "Mark This post as a helpful / Expert post" link at the bottom of the appropriate reply

Rgds
Geoff
"Some cause happiness wherever they go; others whenever they go."
-Oscar Wilde
 
Gosh! Just to rearrange cell contents? I have something similar and im afraid to ask now but i'll ask anyway.

My data is currently arranged as thus:-


14-Apr-03 868.30 885.26 868.30 885.23
11-Apr-03 871.58 883.34 865.92 868.30
10-Apr-03 865.99 871.78 862.76 871.58
09-Apr-03 878.29 887.35 865.72 865.99
08-Apr-03 879.93 883.11 874.68 878.29


but i want them rearranged such that the row 14-Apr-03 is at the bottom as thus:

08-Apr-03 879.93 883.11 874.68 878.29
09-Apr-03 878.29 887.35 865.72 865.99
10-Apr-03 865.99 871.78 862.76 871.58
11-Apr-03 871.58 883.34 865.92 868.30
14-Apr-03 868.30 885.26 868.30 885.23


Is there any easier way of doing this other. Cutting and pasting will be an extremely tedious option.

Thanks!


Who's that girl! :)
 
lola - this is a very different question than the one posed by Francois - and it has a very simple answer - just select your data (all of it)
and goto Data>Sort
If you have headers, tick the option button for this on the form that pops up and then choose sort by your date column ASCENDING
that's all

Rgds
Geoff
"Some cause happiness wherever they go; others whenever they go."
-Oscar Wilde
 
You are such a star!! And thanks for the quick feedback. I had tried that but hadnt been selecting ALL the data, so i found it had been arranging the figures too in ascending/descending order.

Its now worked like magic and I can rearrange my hair now as i have been banging my head for at least an hour now trying to figure it out :). I can now move to Step 2.

Thanks xlbo!! Thanks everyone.

Who's that girl! :)
 
One quick tip in selecting all the records in a table:

Click on the top right cell in your table

Press Ctrl and the * key on the number pad together

and the whole table will be highlighted.

(you can use Ctrl plus Shift plus 8 key if not using the num key pad)

Regards

Keith
 
Just to throw one more into the pot, the sort on ascending worked because you had dates that were in a logical order as your first column of data. If you didn't have that yet still wanted to do the same thing, you can simply insert a helper column, fill down with 1,2,3,4.... etc and then select that plus your data and do a sort based on the helper column, switching to descending.

Regards
Ken...............
 
Duly noted and taken onboard, Ken.
Thanks!

Who's that girl! :)
 
Hi!
Back to the original question and the answer from Danomaniac. My data is now structured like this (1, 2, 3 ... are still words and not numbers - I have 8 columns (A->H and 7 more columns (1>7))

A B C 1 2 3
A C D 3 4 5
E F F 1 3 4

and so on

I would like the data to be dispatched in separate worksheets so that

in worksheet1
A B C
E F F

in worksheet2
A B C

in worksheet3
A B C
A C D
E F F

in worksheet4
A C D
E F F

and in worksheet5
A C D

can someone help me with that?
Thanks a lot!

Francois
 
Francois,
I'm sorry, but I'm a bit confused by your request. Are you saying you have a total of 15 columns?

Or did you mean 8 rows and 7 columns?

As far as sending the data to different sheets, Excel has some built in data sorting and filtering capabilities that may suffice.

could you try to rephrase your question, maybe expand your example a bit, or even email me a copy of your worksheet and I'll have a look.

Dan
danomaniac@hotmail.com



"It's more like it is now, than it ever has been."
 
Hi Danomaniac!
I do mean 15 columns. Each line is structured like this:
Name/forname/title/reference/referens2/status1/status2/status3/attribute1/attribute2/attribute3/attribute4/attribute5/attribute6/attribute7
I have more than 60 values for the attributes and they are randomely placed in the attributes columns. I need to have worksheets named after all the attributes while the content of each worksheet beeing Name/forname/title/reference/referens2/status1/status2/status3/
mmm. I guess this is getting pretty messy to explain. I send you a copy on Tuesday!
Happy Easter
Francois
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top