Solved

Microsoft Excel - Loop or Macro

Posted on 2013-05-17
9
346 Views
Last Modified: 2013-05-17
I have 250 companies on an excel spreadsheet listed accross on a row. My companies are named Co A, Co B and so on. How do I insert two blank columns before each company. I think this can be done with a macro or a Loop. Below is how the companies are currently listed in each column and How I need them.


Current                              
Co A      Co B      Co C                  
                              
Need                              
Blank      Blank      Co A      Blank      Blank      Co B
0
Comment
Question by:Conernesto
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 3

Expert Comment

by:gnazareth
ID: 39175232
Try this

Sub InsertBlankColumns()
    Set cell = Cells(1, 1)
    
    While cell.Value <> ""
        cell.EntireColumn.Insert Shift:=xlRight
        cell.EntireColumn.Insert Shift:=xlRight
        Set cell = cell.Offset(0, 1)
    Wend
End Sub

Open in new window

0
 

Author Comment

by:Conernesto
ID: 39175254
I have not done this before. How do I/where do I enter this?
0
 
LVL 34

Expert Comment

by:Norie
ID: 39175264
Try this, change the start row and no of columns as required.
Option Explicit

Sub InsertColumns()
Dim rng As Range
Dim HeaderRow As Long
Dim NoColumns As Long

    HeaderRow = 1
    NoColumns = 3


    Set rng = Range("A" & HeaderRow)

    While rng.Value <> ""
        If rng.Value <> "" Then
            rng.Resize(, NoColumns).EntireColumn.Insert xlShiftToRight
        End If

        Set rng = rng.Offset(, 1)
    Wend
    
End Sub

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:gnazareth
ID: 39175267
Make sure your spreadsheet is open
Press Alt + F11
Right click on "Microsoft Excel Objects" -> Insert -> Module
Paste in the code
Sub InsertBlankColumns()
    Set cell = Cells(1, 1)
    
    While cell.Value <> ""
        cell.EntireColumn.Insert Shift:=xlRight
        cell.EntireColumn.Insert Shift:=xlRight
        Set cell = cell.Offset(0, 1)
    Wend
End Sub

Open in new window


Press F5 to run
0
 

Author Comment

by:Conernesto
ID: 39175271
OK. It worked.
0
 

Author Comment

by:Conernesto
ID: 39175363
For the columns inserted can a name for each column be inserted?
I want the first blank column Named "I O" and the second blank column "CmbGrp"

conernesto
0
 
LVL 34

Expert Comment

by:Norie
ID: 39175376
This will add the headers for the new columns.
Option Explicit
Sub InsertColumns()
Dim rng As Range
Dim HeaderRow As Long
Dim NoColumns As Long

    HeaderRow = 1
    NoColumns = 2


    Set rng = Range("A" & HeaderRow)

    While rng.Value <> ""
        If rng.Value <> "" Then
            rng.Resize(, NoColumns).EntireColumn.Insert xlShiftToRight
            rng.Offset(, -2).Resize(, 2).Value = Array("I O", "CmbGrp")
        End If

        Set rng = rng.Offset(, 1)
    Wend
    
End Sub

Open in new window

0
 

Author Comment

by:Conernesto
ID: 39175409
When I run the code to insert the two columns and label the two columns, I get a compile error "ambigous name detected:InsertColumns"

Below is the full code that I have:

Option Explicit

Sub InsertColumns()
Dim rng As Range
Dim HeaderRow As Long
Dim NoColumns As Long

    HeaderRow = 1
    NoColumns = 2


    Set rng = Range("A" & HeaderRow)

    While rng.Value <> ""
        If rng.Value <> "" Then
            rng.Resize(, NoColumns).EntireColumn.Insert xlShiftToRight
        End If

        Set rng = rng.Offset(0, 1)
    Wend
   
End Sub

Option Explicit
Sub InsertColumns()
Dim rng As Range
Dim HeaderRow As Long
Dim NoColumns As Long

    HeaderRow = 1
    NoColumns = 2


    Set rng = Range("A" & HeaderRow)

    While rng.Value <> ""
        If rng.Value <> "" Then
            rng.Resize(, NoColumns).EntireColumn.Insert xlShiftToRight
            rng.Offset(, -2).Resize(, 2).Value = Array("I O", "CmbGrp")
        End If

        Set rng = rng.Offset(, 1)
    Wend
   
End Sub
0
 
LVL 34

Accepted Solution

by:
Norie earned 500 total points
ID: 39175424
That code would replace the previous code.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question