From: Monarch on 17 Feb 2010 10:53 Since i do not know if you have any unique identification of columns, I'll assume that in first cell (row 1) there are letters for columns... Sub MoveandCountColumns() 10 For i = 1 To 254 'As 255 is maximum number of columns If Len(Cells(1, i).Value) = 0 Then Exit For 'This is end of columns If Columns(Cells(1, i).Value).Column <> i Then Columns(Cells(1, i).Value).Select Selection.Copy Columns(255).Select ActiveSheet.Paste Columns(i).Select Selection.Copy Columns(Cells(1, i).Value).Select ActiveSheet.Paste Columns(255).Select Selection.Copy Columns(i).Select ActiveSheet.Paste GoTo 10 End If Next I'm usind column 255 as temporary column for data that should be removed. I'll try to explain... If in first column you have C and in third you have E then i'll copy E column to column 255 then copy first column to its right place and then put back column 255 content in first columns... before macro i had C A E, after i have E X C, what means that i C is in right place so i start from begin again until all columns are in right places. Here you will need some loops to identify what column on what place you will need. If you can provide On 17.02.2010 14:34, avi wrote: > Hi, > > I need to rearrange columns on many sheets in a certain order based on > on column header. > For example, my columns might come into my spreadsheet as > C,B,D,G,A,H,I,F , or as F,B,C,A,D etc. > I want them all sorted say A,B,C,D - the rest of the columns is > irrelevant. > > How do I accomplish this ? > TIA
From: Monarch on 18 Feb 2010 10:48 Ok, here is solution Sub MoveandCountColumnsA() 10 For i = 1 To 254 'As 255 is maximum number of columns If Len(Cells(1, i).Value) = 0 Then Exit For 'This is end of columns col = ColumnPlace(Cells(1, i).Value, i) If col <> i Then Columns(col).Select Selection.Copy Columns(255).Select ActiveSheet.Paste Columns(i).Select Selection.Copy Columns(col).Select ActiveSheet.Paste Columns(255).Select Selection.Copy Columns(i).Select ActiveSheet.Paste GoTo 10 End If Next End Sub and ColumnPlace Function. This function determine what is column identifier and in which column should it move data. You can add in CASE what is column header and where you wish it to put that column. Do not chage "Case else" part... Public Function ColumnPlace(ColumnName, CurrColumn) Select Case ColumnName Case "NUM" ColumnPlace = 1 Case "SYS" ColumnPlace = 2 Case "DIA" ColumnPlace = 3 Case "TAG" ColumnPlace = 4 Case "VAR2" ColumnPlace = 5 Case Else ColumnPlace = CurrColumn End Select End Function This code will move every column on its needed place only if that column is mentioned in ColumnPlace function On 17.02.2010 18:24, avi wrote: > Hi Monarch, > I want to arrange couple of columns across all the sheets in the same > way since I have them now randomly placed across sheets in different > columns. Namely these are: "NUM", "SYS", "DIA", "TAG" etc... > > Your code works perfectly on one sheet where 1st row (as you said) has > letters but can you help me with this. > TIA > >
From: Monarch on 19 Feb 2010 03:57 here is complete solution Does not stop on blank cell (it stops if there are 3 (userdefined) blank cell in a row) If works on all sheets in a workbook Custom list can be arranged in ColumnPlace function (just insert new case line with whatever content of column and desired place of column here is code: Sub MoveandCountColumnsA() Static CountBlanks 'Next two lines cycle thru worksheets For Each a In Worksheets a.Activate 10 For i = 1 To 254 'As 255 is maximum number of columns If Len(Cells(1, i).Value) = 0 Then CountBlanks = CountBlanks + 1 'If there is 3 columns in a row that was blank assume last column If CountBlanks > 3 Then '3 blanks in a row, assume we are over last column Exit For End If Else 'Reset counter if there is only 1 or 2 blank cells CountBlanks = 0 End If col = ColumnPlace(Cells(1, i).Value, i) If col <> i Then Columns(col).Select Selection.Copy Columns(255).Select ActiveSheet.Paste Columns(i).Select Selection.Copy Columns(col).Select ActiveSheet.Paste Columns(255).Select Selection.Copy Columns(i).Select ActiveSheet.Paste GoTo 10 End If Next Next End Sub Public Function ColumnPlace(ColumnName, CurrColumn) Select Case ColumnName Case "NUM" 'Put here content of first cell of column ColumnPlace = 1 'Put here desired place of column Case "SYS" ColumnPlace = 2 Case "DIA" ColumnPlace = 3 Case "TAG" ColumnPlace = 7 Case "VAR2" ColumnPlace = 5 Case Else ColumnPlace = CurrColumn End Select End Function And avi, you are welcome, if there is anything you would like me to change, just ask :) On 18.02.2010 19:29, avi wrote: > Hi, > > While I was waiting on a reply, I modified the code found on > http://www.pcreview.co.uk/forums/thread-2587219.php > in such way that I am pasting copied columns from clipboard by > inserting it to the left of the first column (A). > It also works but I can't manage to run it across all the sheets in > workbook. > Note: there is a limitation in my code (as far I can see) that first > column in all the sheets needs to have the same header. Otherwise, if > the first column is to be pasted on the left of it - there's error. (I > don't have code with me now, but will post it next week when I am back > at work. Basically it is very close to the code on the link above )... > Luckily, all my sheets start with the same header in column A so I am > able to arrange it. > > The code you posted ( thanx very much for it ) works perfectly on one > sheet. > However there is also limitation that there shall be no blank column > between columns of interest ie if there is blank on between, say "NUM" > & "SYS" nothing happens. > Also, please note that I wanted to run it across all the sheets in a > workbook.
|
Pages: 1 Prev: text box in userforms and international number format Next: Extract data from csv file |