Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Microsoft Excel - Loop or Macro

Posted on 2013-05-17
9
Medium Priority
?
354 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 total points
ID: 39175424
That code would replace the previous code.
0

Featured Post

Independent Software Vendors: 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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

722 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