Solved

Microsoft Excel - Loop or Macro

Posted on 2013-05-17
9
309 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
  • 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 33

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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 33

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 33

Accepted Solution

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now