ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   FindTextInsertCol Excel (https://www.excelbanter.com/excel-programming/436905-findtextinsertcol-excel.html)

Langdon

FindTextInsertCol Excel
 
How do I insert a column after a specific text is found in the first row
(i.e. the number 1) for every time it appears in a worksheet?

Bernie Deitrick

FindTextInsertCol Excel
 
Langdon,

You would need to loop like in the example below.

HTH,
Bernie
MS Excel MVP


Sub InsertColumnsAfter1()
Dim FirstAdd As String
Dim C As Range

If Range("A1").Value = 1 Then
Set C = Range("A1")
Else
Set C = Rows("1:1").Find(What:="1", LookAt:=xlWhole)
End If

If Not C Is Nothing Then
FirstAdd = C.Address
C.Offset(0, 1).EntireColumn.Insert
Else
Exit Sub
End If
Set C = Rows("1:1").FindNext(after:=C)
While C.Address < FirstAdd
C.Offset(0, 1).EntireColumn.Insert
Set C = Rows("1:1").FindNext(after:=C)
Wend

End Sub




"Langdon" wrote in message
...
How do I insert a column after a specific text is found in the first row
(i.e. the number 1) for every time it appears in a worksheet?



Rick Rothstein

FindTextInsertCol Excel
 
Here is an alternate macro that does the same thing...

Sub InsertColumnsAfter1()
Dim X As Long, R As Range
For X = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
If Cells(1, X).Value = 1 Then
If R Is Nothing Then
Set R = Columns(X)
Else
Set R = Union(R, Columns(X))
End If
End If
Next
R.Offset(0, 1).Insert
End Sub

--
Rick (MVP - Excel)


"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Langdon,

You would need to loop like in the example below.

HTH,
Bernie
MS Excel MVP


Sub InsertColumnsAfter1()
Dim FirstAdd As String
Dim C As Range

If Range("A1").Value = 1 Then
Set C = Range("A1")
Else
Set C = Rows("1:1").Find(What:="1", LookAt:=xlWhole)
End If

If Not C Is Nothing Then
FirstAdd = C.Address
C.Offset(0, 1).EntireColumn.Insert
Else
Exit Sub
End If
Set C = Rows("1:1").FindNext(after:=C)
While C.Address < FirstAdd
C.Offset(0, 1).EntireColumn.Insert
Set C = Rows("1:1").FindNext(after:=C)
Wend

End Sub




"Langdon" wrote in message
...
How do I insert a column after a specific text is found in the first row
(i.e. the number 1) for every time it appears in a worksheet?





All times are GMT +1. The time now is 03:40 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com