vba code to insert columns in missing numbers horizontally in a range.

mvaldez

New Member
Joined
Jan 11, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I need your valuable help so that the following macro inserts columns in the missing numbers horizontally (in a row) of a selected range.

What happens is that the following macro does not insert the columns when there are other adjacent ranges on the left or right side of the selected range.

Sub InsertColsForMissingNumbers()

Dim rngMyRange As Range
Dim x As String
Dim y As Integer
Dim i As Long, j As Long

Set rngMyRange = Selection

x = rngMyRange.Cells(1, 1).Address

z = rngMyRange.Cells(rngMyRange.Rows.Count, rngMyRange.Columns.Count).Address

y = Range(x).Row

MsgBox "Primer rango: " & x
MsgBox "Segundo rango: " & z
MsgBox "Número de fila: " & y
MsgBox "Primer valor: " & Range(x).Value
MsgBox "Segundo valor: " & Range(z).Value

For i = Range(x).Value To Range(z).Value
j = Cells(y, i + 1) - Cells(y, i)
If j > 1 Then
Columns(i + 1).Resize(, j - 1).EntireColumn.Insert Shift:=xlToRight
i = i + j - 1
End If
Next i

End Sub

What is wrong that doesn't insert the columns?
 

Attachments

  • Insert_columns_in_missing_numbers_horizontally_in_a_range-.jpg
    Insert_columns_in_missing_numbers_horizontally_in_a_range-.jpg
    207.3 KB · Views: 3

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
64,110
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
How about
VBA Code:
Sub InsertColsForMissingNumbers()

Dim rngMyRange As Range
Dim i As Long, j As Long

Set rngMyRange = Selection

For i = rngMyRange.Columns.Count To 2 Step -1
   j = rngMyRange(, i).Value - rngMyRange(, i - 1).Value
   If j > 1 Then rngMyRange(, i).Resize(, j - 1).EntireColumn.insert
Next i

End Sub
 
Solution

mvaldez

New Member
Joined
Jan 11, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Oh my God!

It's amazing!

You have saved me hours of work. Now I can send the information to farmers in the field about the phenological stages of crops more quickly.

Excellent work Fluff!

I really appreciate you help!

Thanks a lot!
Hi & welcome to MrExcel.
How about
VBA Code:
Sub InsertColsForMissingNumbers()

Dim rngMyRange As Range
Dim i As Long, j As Long

Set rngMyRange = Selection

For i = rngMyRange.Columns.Count To 2 Step -1
   j = rngMyRange(, i).Value - rngMyRange(, i - 1).Value
   If j > 1 Then rngMyRange(, i).Resize(, j - 1).EntireColumn.insert
Next i

End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
64,110
Office Version
  1. 365
Platform
  1. Windows
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: vba code to insert columns in missing numbers horizontally in a range.
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 

Forum statistics

Threads
1,144,614
Messages
5,725,319
Members
422,613
Latest member
salim9696

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
Top