Insert range multiple times in VBA

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Insert range multiple times in VBA

Post by YasserKhalil »

Hello everyone
I have a line of code

Code: Select all

sh.Range("D7:D" & lr).Insert Shift:=xlToRight
This will move the range to be E7:E & lr ..
How can I repeat and move the range multiple times so as to make the range reach column I?

I could do it using this line

Code: Select all

sh.Range("D7:D" & lr).Cut sh.Range("D7:D" & lr).Offset(, 5)
But I welcome any more ideas ..

User avatar
HansV
Administrator
Posts: 78393
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Insert range multiple times in VBA

Post by HansV »

Like this:

Code: Select all

    Dim rng As Range
    Set rng = sh.Range("D7:D" & lr)
    Do
        rng.Insert Shift:=xlToRight
    Loop Until rng.Column = 9
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Insert range multiple times in VBA

Post by YasserKhalil »

Thanks a lot, my tutor.
Best Regards

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: Insert range multiple times in VBA

Post by Doc.AElstein »

Hello Yasser,
I am just a little curious, that's all…
Are you sure you want what Hans’s macro does?

If this is your before worksheet,
http://i.imgur.com/I4IjZz9.jpg
UnmovedD.JPG
Then, if you apply Hans macro in this form,

Code: Select all

 Sub Insertses() '  https://eileenslounge.com/viewtopic.php?p=280700#p280700
Dim Sh As Worksheet: Set Sh = ActiveSheet
Dim Lr As Long: Let Lr = 9
Dim rng As Range
 Set rng = Sh.Range("D7:D" & Lr)
    Do
     rng.Insert Shift:=xlToRight
    Loop Until rng.Column = 9
End Sub
Then this is your result:
http://i.imgur.com/VJY5DvW.jpg
MovedD.JPG
If that is what you want, then fair enough. No Problem. - It just seems a bit strange to me that you want to do that.
So I was just checking, that’s all

Alan

BTW, these macros will produce the same final results

Code: Select all

 Sub InsertOnce()
Dim Sh As Worksheet: Set Sh = ActiveSheet
Dim Lr As Long: Let Lr = 9
Dim rng As Range
 Set rng = Sh.Range("D7:H" & Lr)
 rng.Insert Shift:=xlToRight
End Sub

Code: Select all

Sub Insertseses2() '  https://eileenslounge.com/viewtopic.php?p=280700#p280700
Dim Sh As Worksheet: Set Sh = ActiveSheet
Dim Lr As Long: Let Lr = 9
Dim rng As Range
 Set rng = Sh.Range("D7:D" & Lr)
 rng.Resize(, 5).Insert Shift:=xlToRight
End Sub
You do not have the required permissions to view the files attached to this post.
Last edited by Doc.AElstein on 12 Feb 2021, 20:28, edited 1 time in total.
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Insert range multiple times in VBA

Post by YasserKhalil »

Yes Hans' trick does what I need exactly and thanks a lot for the contribution.

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: Insert range multiple times in VBA

Post by Doc.AElstein »

OK
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also