View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Monarch[_2_] Monarch[_2_] is offline
external usenet poster
 
Posts: 12
Default Excel - Macro to rearrange/add columns in all the sheets basedon column header

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 a "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