ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Macro to find and move column (https://www.excelbanter.com/excel-discussion-misc-queries/205474-macro-find-move-column.html)

Andrea

Macro to find and move column
 
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

JCS

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



All times are GMT +1. The time now is 07:40 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com