Copy last used column to next available

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Copy last used column to next available

Post by VegasNath »

I am trying to copy the last used column to the next column over, something like........ (without success)

Code: Select all

iCol = Mid(Cells(1, 256).End(xlToLeft).Columns.Address, 2, 1)
    wb2.Sheets("Report").Range(iCol & "1:" & iCol & "1000").Copy Destination:=(wb2.Sheets("Report").Range(iCol + 1 & "1"))
I guess that I cannot "add 1" to iCol as iCol is a letter (string), not a number, but I am unsure how to achieve the result?

Also tried:

Code: Select all

iCol = Mid(Cells(1, 256).End(xlToLeft).Columns, 1, 1)
    wb2.Sheets("Report").Columns(iCol).Copy Destination:=wb2.Sheets("Report").Columns(iCol) + 1
:wales: Nathan :uk:
There's no place like home.....

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: Copy last used column to next available

Post by VegasNath »

I got there in the end.....

Code: Select all

    Dim i As Integer
    i = wb2.Sheets("Report").Range("IV1").End(xlToLeft).Column
    wb2.Sheets("Report").Columns(i).Copy Destination:=wb2.Sheets("Report").Columns(i + 1)

:cheers: :cheers:
:wales: Nathan :uk:
There's no place like home.....

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: Copy last used column to next available

Post by VegasNath »

Where's the button to give myself a :thumbup:

:evilgrin:
:wales: Nathan :uk:
There's no place like home.....

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: Copy last used column to next available

Post by VegasNath »

VegasNath wrote:I got there in the end.....

Code: Select all

    Dim i As Integer
    i = wb2.Sheets("Report").Range("IV1").End(xlToLeft).Column
    wb2.Sheets("Report").Columns(i).Copy Destination:=wb2.Sheets("Report").Columns(i + 1)

:cheers: :cheers:
AAAaaaaaaaaaaargh. I still need to refer to the column letter. How can i do that?
:wales: Nathan :uk:
There's no place like home.....

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: Copy last used column to next available

Post by VegasNath »

VegasNath wrote:
VegasNath wrote:I got there in the end.....

Code: Select all

    Dim i As Integer
    i = wb2.Sheets("Report").Range("IV1").End(xlToLeft).Column
    wb2.Sheets("Report").Columns(i).Copy Destination:=wb2.Sheets("Report").Columns(i + 1)

:cheers: :cheers:
AAAaaaaaaaaaaargh. I still need to refer to the column letter. How can i do that?
Probably not the best approach, but this works:

Code: Select all

Dim i As Integer
Dim iCol As String

    If Mid(Cells(1, 256).End(xlToLeft).Columns.Address, 4, 1) <> "$" Then
        iCol = Mid(Cells(1, 256).End(xlToLeft).Columns.Address, 2, 1)
    Else
        iCol = Mid(Cells(1, 256).End(xlToLeft).Columns.Address, 2, 2)
    End If

    i = wb2.Sheets("Report").Range("IV1").End(xlToLeft).Column
    wb2.Sheets("Report").Columns(i).Copy Destination:=wb2.Sheets("Report").Columns(i + 1)
    wb2.Sheets("Report").Range(iCol & "1").Offset(0, 1).Interior.ColorIndex = xlNone
    wb2.Sheets("Report").Range(iCol & "1").Offset(0, 1).ClearComments
:wales: Nathan :uk:
There's no place like home.....

User avatar
HansV
Administrator
Posts: 78657
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy last used column to next available

Post by HansV »

There's no need to know the column letter(s). Instead of Range(iCol & "1"), you can use Cells(1, i).
Best wishes,
Hans

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: Copy last used column to next available

Post by VegasNath »

Thanks Hans, much simpler!
:wales: Nathan :uk:
There's no place like home.....