Select columns using "for" - beginner guide

makiwara

Board Regular
Joined
Mar 8, 2018
Messages
171
Hi! Im stuck with this problem, it would be great if you could help me :-)


For o = 3 To Z
Columns(o).Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Next

________

I want to select the column C,D,E and so on, up to the Z variable, and then insert 2 new columns before every selected column. Could you help me? I try to figure out this for a half hour:D Have a nice day!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try
Code:
For o = Z To 3 Step -1
   Columns(o).Resize(, 2).Insert
Next o
 
Upvote 0
Hi Fluff, thank you for your answer!

It doesn't work, I suspect that I couldn't tell you clearly what i'm looking for.
https://ibb.co/m4rLQd
Can you help me? Have a nice day!

m4rLQd


Try
Code:
For o = Z To 3 Step -1
   Columns(o).Resize(, 2).Insert
Next o
 
Upvote 0
In what way doesn't it work? If I start off with this


Excel 2013 32 bit
ABCDEFGH
1PostcodeIn Use?LatitudeLongitudeEastingNorthingGridRefCounty
2AL10 9WXYes51.750259-0.234901521944207203TL219072Hertfordshire
3AL6 0PBYes51.829624-0.16166526777216154TL267161Hertfordshire
4AL6 0PEYes51.83425-0.168206526313216657TL263166Hertfordshire
5AL6 9TUYes51.83911-0.223619522482217103TL224171Hertfordshire
6AL6 9UGYes51.855995-0.206283523630219010TL236190Hertfordshire
7AL6 9UJYes51.840265-0.223907522459217231TL224172Hertfordshire
8AL7 1UUNo51.816179-0.194575524546214602TL245146Hertfordshire
9AL7 2ZYNo51.799543-0.170106526279212794TL262127Hertfordshire
10AL7 3JHYes51.792809-0.178267525735212031TL257120Hertfordshire
Postcodes (2)


I end up with


Excel 2013 32 bit
ABCDEFGHIJKLMN
1PostcodeIn Use?LatitudeLongitudeEastingNorthing
2AL10 9WXYes51.750259-0.234901521944207203
3AL6 0PBYes51.829624-0.16166526777216154
4AL6 0PEYes51.83425-0.168206526313216657
5AL6 9TUYes51.83911-0.223619522482217103
6AL6 9UGYes51.855995-0.206283523630219010
7AL6 9UJYes51.840265-0.223907522459217231
8AL7 1UUNo51.816179-0.194575524546214602
9AL7 2ZYNo51.799543-0.170106526279212794
10AL7 3JHYes51.792809-0.178267525735212031
Postcodes (2)
 
Upvote 0
Try this:
Code:
Sub Insert_Columns()
'Modified 5/9/2018 5:10 PM  EDT
Application.ScreenUpdating = False
    For i = 26 To 3 Step -1
        Columns(i).Resize(, 2).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Dear Fluff, I tried your code in a new workbook with random data, and it worked, so you have right.

But in my current project it's not, and I don't know why.
XD3QvFQ
https://imgur.com/a/XD3QvFQ

The BASIC data in my worksheet is in Column A and every other cell in the worksheet is EMPTY!
The VBA code fills them with data, so it makes the splitting of the strings too. But unfortunately when I insert you code, it runs, but gives no result. How can it be possible, that here it doesn't works? Thank you for your help, i am really lost now.
 
Upvote 0
Thank you, I dont now what was the problem, but it works fine with this code. Thank you for your help both have a nice day :-)

Try this:
Code:
Sub Insert_Columns()
'Modified 5/9/2018 5:10 PM  EDT
Application.ScreenUpdating = False
    For i = 26 To 3 Step -1
        Columns(i).Resize(, 2).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.
Thank you, I dont now what was the problem, but it works fine with this code. Thank you for your help both have a nice day :-)
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

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