Home |
Search |
Today's Posts |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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. |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Rearrange columns | Excel Programming | |||
Macro to rearrange/add columns based on column header? | Excel Programming | |||
Rearrange columns using VBA | Excel Programming | |||
Macro help to rearrange excel table | Excel Programming | |||
hide columns based on header macro | Excel Programming |