Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Column Flipping Range changed 1

Status
Not open for further replies.

CrystalStart

IS-IT--Management
Feb 3, 2005
185
US
I am using this code to flip data from starting at column 2 to the end of data.
It works a treat.
But recently I need to start not only at column 2 but also at Range A5 of column 2.
Do you see how to alter this code to make it work?

lColumnCount = Sheets(i).[B:B].CurrentRegion.Columns.Count


If lColumnCount = "" Then
MsgBox "There is no data."
Exit Sub
End If
Application.ScreenUpdating = False
iStart = 2
iEnd = lColumnCount
Do While iStart < iEnd
vTop = Selection.Columns(iStart)
vEnd = Selection.Columns(iEnd)
Selection.Columns(iEnd) = vTop
Selection.Columns(iStart) = vEnd
iStart = iStart + 1
iEnd = iEnd - 1
Loop


THANKS TO ALL
 
CrystalStart,
Here is some code that restricts the range of data being moved to rows 5:65536. Your code used the Selection, but didn't show how it was established--so I changed that to the data in the current region of column B. I also changed the test for the minimum number of columns, so please test that part.
Code:
Sub ColumnSwitcher()
Dim lColumnCount As Long, iStart As Long, iEnd As Long, i As Long
Dim vTop As Variant, vEnd As Variant
Dim rg As Range

i = ActiveSheet.Index   'Change this statement to suit your situation

With Sheets(i)
    Set rg = .[B:B].CurrentRegion
    If rg.Column <> 1 Then Set rg = Union(rg, rg.Columns(1).Offset(0, -1))
    Set rg = Intersect(rg, .Range("5:65536"))
    lColumnCount = rg.Columns.Count - 1
    If lColumnCount < 2 Then
            MsgBox "There is no data."
            Exit Sub
        End If
       Application.ScreenUpdating = False
            iStart = 2
            iEnd = lColumnCount
            Do While iStart < iEnd
                vTop = rg.Columns(iStart)
                vEnd = rg.Columns(iEnd)
                rg.Columns(iEnd) = vTop
                rg.Columns(iStart) = vEnd
                iStart = iStart + 1
                iEnd = iEnd - 1
            Loop
End With
End Sub
Brad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top