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: 6

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
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
 
Upvote 0
Solution
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,213,501
Messages
6,114,010
Members
448,543
Latest member
MartinLarkin

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