Macro to search, cut, and insert help

GlobalQC1

New Member
Joined
Jun 18, 2012
Messages
3
This is one of my first macros so all help is appreciated. This part of the macro searches for a column header then cuts it and places it in a different column location. I wrote it to paste the column and it works fine but I need it to now insert the column instead of pasting it and I can't seem to get the code right. This is what I have been using:


For Each rng In Range("A5:IV5")

If rng.Value = "Src Type" Then
rng.EntireColumn.Cut
Sheets("OBLog_Formatted").Select
Columns("D:D").Select
ActiveSheet.Paste
End If

If rng.Value = "Date" Then
rng.EntireColumn.Cut
Sheets("OBLog_Formatted").Select
Columns("E:E").Select
ActiveSheet.Paste
End If

If rng.Value = "Line Name" Then
rng.EntireColumn.Cut
Sheets("OBLog_Formatted").Select
Columns("F:F").Select
ActiveSheet.Paste
End If

Next rng


Is there an easy way to convert this to insert instead?
Thanks
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Re: Marco to search, cut, and insert help

Maybe

Code:
If Rng.Value = "Src Type" Then
Rng.EntireColumn.Cut
Sheets("OBLog_Formatted").Columns("D").Insert
 
Upvote 0
Re: Marco to search, cut, and insert help

Two problems with that:

1) If the column happens to be in the correct location already it will fail.

2) The columns don't seem to be placing correctly. How does a .Insert shift the columns?
 
Upvote 0
Re: Marco to search, cut, and insert help

I'll explain more of what I am trying to do:

I receiver data organized into columns with headers each day. The problem is the columns are never in the same order. I need 13 specific columns moved to the front in a certain order. My search and paste code works well except if one of the later columns gets pasted over by one of the earlier columns. That's why I want to change it to insert so the columns just get shifted to the right and can still be found by the search instead of pasted over.
 
Upvote 0

Forum statistics

Threads
1,203,270
Messages
6,054,488
Members
444,727
Latest member
Mayank Sharma

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top