View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
JCS JCS is offline
external usenet poster
 
Posts: 93
Default Macro to find and move column

Andrea,

Try the following code. Copy and paste into the Microsoft Visual Basic
Editor. This code assumes that data are in contiguous columns. Please note
where you have to make changes to code. There are 3 places.

HTH.
John

Sub MoveColumn()
'

Dim FindWhat As Variant
Dim Col As Variant
Dim ColEnd As Variant
Dim ColLast As Variant

'Determines what column is to be moved and what column to move it to.

Application.ScreenUpdating = False
Range("a1").Select 'Replace with address of upper
corner of your table
Selection.CurrentRegion.Select
ColEnd = Selection.Columns.Count
ColLast = Mid(ActiveCell.Offset(0, ColEnd).Address, 2, 1)
Range("a1").Select 'Replace with address of upper
corner of your table

'Edit Find to search for string

FindWhat = Application.InputBox("Text", "Text")
Cells.Find(What:=FindWhat, After:=ActiveCell, LookIn:=xlFormulas,
LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False _
, SearchFormat:=False).Activate


'Moves column based on search string to column nect last column.

Col = Mid(ActiveCell.Address, 2, 1)
Columns(Col).Select
Selection.Cut Destination:=Columns(ColLast)
Range("a1").Select 'Replace with address of
upper corner of your table
Application.ScreenUpdating = True


End Sub

"andrea" wrote:

Hi
I am wanting to run a macro that
1) finds specific text eg "XYZ"
2) select this entire column and cut
3) paste after the last column

I am using the find function as the column that XYZ appears in will vary in
different spreadsheets. There may be a better way?!

Thanks
Andrea