I can't insert a column in a loop with VBA

mvaldez

New Member
Joined
Jan 11, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello dear experts.

I would greatly appreciate your help with this problem that I have to insert a column to the left in the row if below the value in Range(x) of the cell is not equal to one. I have tried many ways, but the routine inserts many columns to the right.

Could you help me with this case, please?

In advance, I am very grateful for your valuable help.

The code I wrote is the following:

Sub InsertColumns()

Dim myCell As Range
Dim MyRange As Range

Set MyRange = ActiveSheet.Rows(5).SpecialCells(xlCellTypeConstants, xlNumbers)
For Each myCell In MyRange
x = myCell.Address(False, False)
MsgBox "x: " & x

Range(x).Select

For i = 1 To 1 Step -1
With Range(x)
.EntireColumn.Insert
.Offset(0, -1).ColumnWidth = 5
End With
Next

Next

Range("A1").Select

End Sub

Inserting Columns.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAU
5200820132014201620172018
6DepartamentoMunicipio2111121314151635678123456789141234567891234567891011121314
7Baja VerapazCubulco000000000
8Baja VerapazEl Chol000000000
9Baja VerapazGranados000000000
10Baja VerapazPurulha000000000
11Baja VerapazRabinal000000000
12Baja VerapazSalama000000000
13Baja VerapazSan Jeronimo000000000
14Baja VerapazSan Miguel Chicaj000000000
Cosecha 3


Thank you very mcuh in advance.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Perhaps something like this.

VBA Code:
Sub InsertColumns()
    
    Dim myCell As Range
    Dim MyRange As Range
    Dim S As String, V
    
    Set MyRange = ActiveSheet.Rows(5).SpecialCells(xlCellTypeConstants, xlNumbers)
    For Each myCell In MyRange
        V = myCell.Offset(1).Value
        
        S = "     x: " & myCell.Address(False, False) & " = " & myCell.Value & vbCr
        S = S & "x+1: " & myCell.Offset(1).Address(False, False) & " = " & V
        MsgBox S
        
        If V = 1 Then
            myCell.EntireColumn.Insert shift:=xlLeft
        End If
    Next myCell
End Sub
 
Upvote 0
Hello rlv01.

I greatly appreciate your soon answer!

The macro works fine if the value is equal to one, but my problem is that I want to insert a column to the left if the value below the year is NOT equal to one.

When the value is not equal to one, the code inserts many columns to the right.

How could you solve this problem?
 
Upvote 0
I cannot speak for anyone else, but it is not 100% clear to me exactly what you are trying to do and where.
You posted a picture in your original post. Can you walk us through an example of exactly what should happen and where, based on that image?
Or maybe show us another image, of what your example should look like AFTER the macro is done.
 
Upvote 0
The macro works fine if the value is equal to one, but my problem is that I want to insert a column to the left if the value below the year is NOT equal to one.
Still not clear. What if you changed

VBA Code:
      If V = 1 Then

to

VBA Code:
      If V <> 1 Then

Does that do what you want?

BTW, it would help a lot if you would use code tags to post your code:

 
Upvote 0
This is how it should look. If the number one does NOT exist under the year, you must insert a column from the left side in inserting the number one in the new column.

Thank you.

Inserting Columns.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJ
520082013201420162017
6DepartamentoMunicipio121111121314151613567812345678914123456789
7Baja VerapazCubulco000000000
8Baja VerapazEl Chol000000000
9Baja VerapazGranados000000000
10Baja VerapazPurulha000000000
11Baja VerapazRabinal000000000
12Baja VerapazSalama000000000
13Baja VerapazSan Jeronimo000000000
14Baja VerapazSan Miguel Chicaj000000000
Cosecha 3
 
Upvote 0
OK, I think I see now.

Try this:
VBA Code:
Sub MyInsertColumns()

    Dim c As Long
    
    Application.ScreenUpdating = False
    
    For c = Cells(5, Columns.Count).End(xlToLeft).Column To 3 Step -1
        If Cells(5, c) > 0 And Cells(6, c) <> 1 Then
            Columns(c).Insert
            Cells(6, c) = 1
        End If
    Next c
    
    Application.ScreenUpdating = True
    
    MsgBox "Macro complete"
    
End Sub
 
Upvote 0
Solution
Oh my God!

This is wonderful!

You the experts are real genius!

My respects to you!

Finally, there's a solution after trying several weeks and breaking my head!

Thank you very much for your valuable help!
 
Upvote 0
Thank you very much rlv01 and Joe4 for your help!

Have a wonderful day!
 
Upvote 0
You are most welcome!
Glad we were able to help.
:)
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top