Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default 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?


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default 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?



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:14 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"