Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 50
Default Sorting columns - can't maintain column width

I have a worksheet that contains 268 columns with headers. I have a couple
of different ways I want to sort the data left to right by columns. The sort
itself works fine -- the issue is that I can't seem to maintain the integrity
of the original column width for each column of data. For example, let's say
the 'original' column width is as follows:

A = 10 B = 15 C = 5

If I sort the data so that columns B & C become reversed, the data that
'was' in column C now has a width of 15 (too large), while the data from
column B now has a width of 5 (too small). I cannot use autofit afterwards
because the wrapped headers in each column cause the widths to become even
more distorted. I am using VB code to perform the sort function--here is an
example of one of the sorts. Is there something I can do to maintain the
integrity of the column width so it stays with the original data?

Sub Sort_by_Measure()
'Sort columns left to right by Measure, then by Quarter
Application.ScreenUpdating = False
UnhideCols
Columns("E:JZ").Select
ActiveWorkbook.Worksheets("Detail").Sort.SortField s.Clear
ActiveWorkbook.Worksheets("Detail").Sort.SortField s.add
Key:=Range("E6:JZ6") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Detail").Sort.SortField s.add
Key:=Range("E5:JZ5") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Detail").Sort
.SetRange Range("E1:JZ20000")
.Header = xlNo
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
Hide_Detail_Cols
Range("A11").Select
Application.ScreenUpdating = True
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Sorting columns - can't maintain column width

I stored the columns widths in an array before the sort and put the column
number in row 65536 before the sort. then restored the column widths after
the sort


Sub Sort_by_Measure()
Dim ColArray(256)

'Sort columns left to right by Measure, then by Quarter
Application.ScreenUpdating = False
UnhideCols

With ActiveWorkbook.Worksheets("Detail")
For ColCount = 0 To 255
'put width in array
ColArray(ColCount) = .Columns(ColCount + 1).Width
'put index in row 65536
.Cells(Rows.Count, ColCount) = ColCount
Next ColCount



.Columns("E:JZ").Sort _
header:=xlNo, _
Orientation:=xlSortColumns, _
key1:=.Range("E5"), _
order1:=xlAscending, _
key2:=.Range("E6"), _
order2:=xlAscending


For ColCount = 0 To 255
'put width in array
.Columns(ColCount + 1).Width = ColArray(ColCount)
Next ColCount
.Rows(Rows.Count).Delete

Hide_Detail_Cols

Application.ScreenUpdating = True
End Sub

"jday" wrote:

I have a worksheet that contains 268 columns with headers. I have a couple
of different ways I want to sort the data left to right by columns. The sort
itself works fine -- the issue is that I can't seem to maintain the integrity
of the original column width for each column of data. For example, let's say
the 'original' column width is as follows:

A = 10 B = 15 C = 5

If I sort the data so that columns B & C become reversed, the data that
'was' in column C now has a width of 15 (too large), while the data from
column B now has a width of 5 (too small). I cannot use autofit afterwards
because the wrapped headers in each column cause the widths to become even
more distorted. I am using VB code to perform the sort function--here is an
example of one of the sorts. Is there something I can do to maintain the
integrity of the column width so it stays with the original data?

Sub Sort_by_Measure()
'Sort columns left to right by Measure, then by Quarter
Application.ScreenUpdating = False
UnhideCols
Columns("E:JZ").Select
ActiveWorkbook.Worksheets("Detail").Sort.SortField s.Clear
ActiveWorkbook.Worksheets("Detail").Sort.SortField s.add
Key:=Range("E6:JZ6") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Detail").Sort.SortField s.add
Key:=Range("E5:JZ5") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Detail").Sort
.SetRange Range("E1:JZ20000")
.Header = xlNo
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
Hide_Detail_Cols
Range("A11").Select
Application.ScreenUpdating = True
End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Increasing width of columns in column chart Shelly Charts and Charting in Excel 2 June 22nd 09 02:36 AM
How to alter width of columns in Column Charts? TimJim Charts and Charting in Excel 2 January 16th 07 01:00 AM
How can I set width of the columns in the column graph? yulia Charts and Charting in Excel 1 December 16th 06 09:04 PM
Set Column Width Based On Total Width Of Other Columns rayneraingoaway Excel Programming 1 June 28th 06 11:10 PM


All times are GMT +1. The time now is 09:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"