Insert multiple columns to the right in sequential order

0 votes

Aim:  selecting many columns, copying them, and then placing each of the copies to the right of the original. Examples include choosing columns A, B, and C, duplicating them in that order, and then creating SIX columns with the formulas A=B, C=D, and E=F.

Any suggestions on how I can change "Shift:=xlToRight" to insert columns to their right rather than at the right end of the full range are welcome.

Feb 4, 2023 in Others by Kithuzzz
• 38,000 points
484 views

1 answer to this question.

0 votes

Duplicate Columns

  • Why these complications?
  • Flexibility. You don't have to select entire columns or adjacent columns. The selected cells can be in any row or column and they can be selected in any order. The line Set rg = Selection.EntireColumn takes care of all issues that might occur without it.
Option Explicit

Sub DuplicateColumns()
    
    If Selection Is Nothing Then Exit Sub ' no selection
    If Not TypeOf Selection Is Range Then Exit Sub ' not a range
    
    Dim rg As Range: Set rg = Selection.EntireColumn
    
    Dim arg As Range, crg As Range, a As Long, c As Long 
    
    For a = rg.Areas.Count To 1 Step -1
        Set arg = rg.Areas(a)
        For c = arg.Columns.Count To 1 Step -1
            Set crg = arg.Columns(c)
            crg.Copy
            crg.Offset(, 1).Insert xlShiftToRight, xlFormatFromLeftOrAbove
        Next c
    Next a
    
End Sub
answered Feb 4, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

How to insert a new row in the specified cell only, of Excel sheet using c#?

I have this worksheet with a matrix ...READ MORE

answered Nov 24, 2022 in Others by narikkadan
• 63,600 points
2,339 views
0 votes
1 answer
0 votes
1 answer

Excel: Is it possible to reorder the data in 2 columns to match up if they have a certain number of characters / a string in common?

Try this: =LET(files,A1:A4, URLs,B1:B4, f,BYROW(files,LAMBDA(r,TEX ...READ MORE

answered Jan 21, 2023 in Others by narikkadan
• 63,600 points
585 views
0 votes
1 answer

How to insert a picture into Excel at a specified cell position with VBA

Try this: With xlApp.ActiveSheet.Pictures.Insert(PicPath) With ...READ MORE

answered Oct 24, 2022 in Others by narikkadan
• 63,600 points
2,487 views
0 votes
1 answer

VBA Insert row instead of copy

Try this: Option Explicit ' declare all variables Sub ...READ MORE

answered Jan 14, 2023 in Others by narikkadan
• 63,600 points
817 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,212 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,663 views
0 votes
1 answer

Create unique rows in Excel with limited data to be used in multiple columns

This setup isn't readily generalizable, though since ...READ MORE

answered Oct 14, 2022 in Others by narikkadan
• 63,600 points
730 views
0 votes
1 answer

How can I use a command button in excel to set the value of multiple cells in one click?

Try this: Private Scan As Integer Private Sub CommandButton1_Click() ...READ MORE

answered Oct 24, 2022 in Others by narikkadan
• 63,600 points
782 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP