ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Loop Until Desired Columns left (https://www.excelbanter.com/excel-programming/401956-loop-until-desired-columns-left.html)

SteveT

Loop Until Desired Columns left
 
Hello,

I'm using JLGWhiz's solution in posting "Deleting Column Based On Header"
to delete columns other than those correctly titled. - Below -

It works Fine but only when run several times or repeated.

Can someone help me enhance to automatically loop until either desired # of
columns with data or until Column Headers indicated below all that left ?

Dim C As Range
lc = Cells(1, Columns.Count).End(xlToLeft).Column
Set myRng = Range("A1", Cells(1, lc))
For Each C In myRng
If C < "Doc. Date" And C < "DocumentN" And C < "Type" And C <
"Reference" And C < "LC amnt" Then
C.EntireColumn.Delete
End If
Next

Many Thanks / Happy Holidays
Steven
all left

Dave Peterson

Loop Until Desired Columns left
 
When you delete rows, it's easier to start at the bottom and work your way to
the top.

When you delete columns, it's easier to start at the far right and work your way
to the left.

Dim iCol As long
dim LastCol as long

with worksheets("Sheetnamehere")
lastcol = .cells(1,.columns.count).end(xltoleft).column
for icol = lastcol to 1 step -1
select case lcase(.cells(1,icol).value)
case is = "doc. date", "type", "reference", "lc amnt"
'skip it
case else
.columns(icol).delete
end select
next icol
end with

Sometimes "select case" makes it a bit easier to see what's going on.

SteveT wrote:

Hello,

I'm using JLGWhiz's solution in posting "Deleting Column Based On Header"
to delete columns other than those correctly titled. - Below -

It works Fine but only when run several times or repeated.

Can someone help me enhance to automatically loop until either desired # of
columns with data or until Column Headers indicated below all that left ?

Dim C As Range
lc = Cells(1, Columns.Count).End(xlToLeft).Column
Set myRng = Range("A1", Cells(1, lc))
For Each C In myRng
If C < "Doc. Date" And C < "DocumentN" And C < "Type" And C <
"Reference" And C < "LC amnt" Then
C.EntireColumn.Delete
End If
Next

Many Thanks / Happy Holidays
Steven
all left


--

Dave Peterson

FSt1

Loop Until Desired Columns left
 
hi
look pretty streight forward to me. what happens if you don't run it
multiple times?

regards
FSt1

"SteveT" wrote:

Hello,

I'm using JLGWhiz's solution in posting "Deleting Column Based On Header"
to delete columns other than those correctly titled. - Below -

It works Fine but only when run several times or repeated.

Can someone help me enhance to automatically loop until either desired # of
columns with data or until Column Headers indicated below all that left ?

Dim C As Range
lc = Cells(1, Columns.Count).End(xlToLeft).Column
Set myRng = Range("A1", Cells(1, lc))
For Each C In myRng
If C < "Doc. Date" And C < "DocumentN" And C < "Type" And C <
"Reference" And C < "LC amnt" Then
C.EntireColumn.Delete
End If
Next

Many Thanks / Happy Holidays
Steven
all left


SteveT

Loop Until Desired Columns left
 
Dave,

Your explanation makes perfect sense and solution works great.

thanks !



"Dave Peterson" wrote:

When you delete rows, it's easier to start at the bottom and work your way to
the top.

When you delete columns, it's easier to start at the far right and work your way
to the left.

Dim iCol As long
dim LastCol as long

with worksheets("Sheetnamehere")
lastcol = .cells(1,.columns.count).end(xltoleft).column
for icol = lastcol to 1 step -1
select case lcase(.cells(1,icol).value)
case is = "doc. date", "type", "reference", "lc amnt"
'skip it
case else
.columns(icol).delete
end select
next icol
end with

Sometimes "select case" makes it a bit easier to see what's going on.

SteveT wrote:

Hello,

I'm using JLGWhiz's solution in posting "Deleting Column Based On Header"
to delete columns other than those correctly titled. - Below -

It works Fine but only when run several times or repeated.

Can someone help me enhance to automatically loop until either desired # of
columns with data or until Column Headers indicated below all that left ?

Dim C As Range
lc = Cells(1, Columns.Count).End(xlToLeft).Column
Set myRng = Range("A1", Cells(1, lc))
For Each C In myRng
If C < "Doc. Date" And C < "DocumentN" And C < "Type" And C <
"Reference" And C < "LC amnt" Then
C.EntireColumn.Delete
End If
Next

Many Thanks / Happy Holidays
Steven
all left


--

Dave Peterson



All times are GMT +1. The time now is 03:08 AM.

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