Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Adjust code to run anywhere on sheet

This code works fine on a list with a Header in column A.
Copies the Header across the same row, starting 1 column to the right for as many columns as there are items in the list.
Cuts the items in the list to the right on a "slant" 1 column over and 1 row down until end of list.

So now the OP says the list will not always be in the same range but wants the code to do as it does here as I have posted. The lists to processed will be in different columns and not always starting at row 1.

I tried using 'For Each c In Selection' but I cannot figure out how to identify the header in the Selection to copy across as needed.

Thanks.
Howard

Option Explicit

Sub cLant()

Dim c As Range
Dim i As Long
Dim rCt As Range

Set rCt = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)

i = 0
For Each c In rCt
c.Cut c.Offset(, i)
Range("A1").Copy Range("A1").Offset(, i)
i = i + 1
Next

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Adjust code to run anywhere on sheet

On Saturday, May 18, 2013 3:33:27 PM UTC-7, Howard wrote:
This code works fine on a list with a Header in column A.

Copies the Header across the same row, starting 1 column to the right for as many columns as there are items in the list.

Cuts the items in the list to the right on a "slant" 1 column over and 1 row down until end of list.



So now the OP says the list will not always be in the same range but wants the code to do as it does here as I have posted. The lists to processed will be in different columns and not always starting at row 1.



I tried using 'For Each c In Selection' but I cannot figure out how to identify the header in the Selection to copy across as needed.



Thanks.

Howard



Option Explicit



Sub cLant()



Dim c As Range

Dim i As Long

Dim rCt As Range



Set rCt = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)



i = 0

For Each c In rCt

c.Cut c.Offset(, i)

Range("A1").Copy Range("A1").Offset(, i)

i = i + 1

Next



End Sub


Small correction:

<Copies the Header across the same row, starting 1 column to the right for as many columns as there are items in the list.

Actually, if list starts in A1, then A1 is the anchor cell and all is displaced from A1. Put a few items in A1 to A5, run code will be a better explanation.

Howard
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default Adjust code to run anywhere on sheet

"Howard" wrote:
So now the OP says the list will not always be in the same
range but wants the code to do as it does here as I have
posted. The lists to processed will be in different columns
and not always starting at row 1.

I tried using 'For Each c In Selection' but I cannot figure
out how to identify the header in the Selection to copy across
as needed.


Minimally, try:

Sub cLant()
Dim c As Range
Dim i As Long
Dim rCt As Range, sel As Range

Set sel = Selection(1)
Set rCt = Range(sel.Offset(1), Cells(Rows.Count, sel.Column).End(xlUp))
i = 0
For Each c In rCt
c.Cut c.Offset(0, i)
sel.Copy sel.Offset(0, i)
i = i + 1
Next
End Sub

That assumes the user selects at least the header cell.

Selection(1) is defensive programming: it ensures that sel references a
single cell (the header cell), even if the user selects multiple cells, even
a rectangular range.

However, that is inefficient because of the use of the clipboard. The
following runs 5.5 times faster on my computer (YMMV), if you can tolerate
the assumptions detailed below.

Sub cLant()
Dim c As Range
Dim i As Long
Dim rCt As Range, sel As Range
Dim h As Variant

Set sel = Selection(1)
Set rCt = Range(sel.Offset(2), Cells(Rows.Count, sel.Column).End(xlUp))
i = 1
h = sel.Formula
For Each c In rCt
c.Offset(0, i).Formula = c.Formula
c.Clear
sel.Offset(0, i).Formula = h
i = i + 1
Next
End Sub

Further simplications and optimizations can be made, depending on additional
assumptions.

Assumptions:

1. There are at least 2 cells under the header to be moved across.

This assumption is due to the use of Offset(2) instead of Offset(1) and i=1
instead of i=0.

If you do not want to make that assumption, some simple tweaks will make it
work. Let us know if you need help with that.

2. The header cell and subsequent cells can contain formulas or constant
values.

Further simplifications could be made if we one or both are not formulas.
But the changes would not improve the run time substantially.

However, if the cells contain formulas, their formats are not copied above.
That is one benefit of using the clipboard.


----- original message -----

"Howard" wrote in message
...
This code works fine on a list with a Header in column A.
Copies the Header across the same row, starting 1 column to the right for
as many columns as there are items in the list.
Cuts the items in the list to the right on a "slant" 1 column over and 1
row down until end of list.

So now the OP says the list will not always be in the same range but wants
the code to do as it does here as I have posted. The lists to processed
will be in different columns and not always starting at row 1.

I tried using 'For Each c In Selection' but I cannot figure out how to
identify the header in the Selection to copy across as needed.

Thanks.
Howard

Option Explicit

Sub cLant()

Dim c As Range
Dim i As Long
Dim rCt As Range

Set rCt = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)

i = 0
For Each c In rCt
c.Cut c.Offset(, i)
Range("A1").Copy Range("A1").Offset(, i)
i = i + 1
Next

End Sub


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Adjust code to run anywhere on sheet

On Saturday, May 18, 2013 5:24:20 PM UTC-7, joeu2004 wrote:
"Howard" wrote:

So now the OP says the list will not always be in the same


range but wants the code to do as it does here as I have


posted. The lists to processed will be in different columns


and not always starting at row 1.




I tried using 'For Each c In Selection' but I cannot figure


out how to identify the header in the Selection to copy across


as needed.




Minimally, try:



Sub cLant()

Dim c As Range

Dim i As Long

Dim rCt As Range, sel As Range



Set sel = Selection(1)

Set rCt = Range(sel.Offset(1), Cells(Rows.Count, sel.Column).End(xlUp))

i = 0

For Each c In rCt

c.Cut c.Offset(0, i)

sel.Copy sel.Offset(0, i)

i = i + 1

Next

End Sub



That assumes the user selects at least the header cell.



Selection(1) is defensive programming: it ensures that sel references a

single cell (the header cell), even if the user selects multiple cells, even

a rectangular range.



However, that is inefficient because of the use of the clipboard. The

following runs 5.5 times faster on my computer (YMMV), if you can tolerate

the assumptions detailed below.



Sub cLant()

Dim c As Range

Dim i As Long

Dim rCt As Range, sel As Range

Dim h As Variant



Set sel = Selection(1)

Set rCt = Range(sel.Offset(2), Cells(Rows.Count, sel.Column).End(xlUp))

i = 1

h = sel.Formula

For Each c In rCt

c.Offset(0, i).Formula = c.Formula

c.Clear

sel.Offset(0, i).Formula = h

i = i + 1

Next

End Sub



Further simplications and optimizations can be made, depending on additional

assumptions.



Assumptions:



1. There are at least 2 cells under the header to be moved across.



This assumption is due to the use of Offset(2) instead of Offset(1) and i=1

instead of i=0.



If you do not want to make that assumption, some simple tweaks will make it

work. Let us know if you need help with that.



2. The header cell and subsequent cells can contain formulas or constant

values.



Further simplifications could be made if we one or both are not formulas.

But the changes would not improve the run time substantially.



However, if the cells contain formulas, their formats are not copied above.

That is one benefit of using the clipboard.





----- original message -----



"Howard" wrote in message

...

This code works fine on a list with a Header in column A.


Copies the Header across the same row, starting 1 column to the right for


as many columns as there are items in the list.


Cuts the items in the list to the right on a "slant" 1 column over and 1


row down until end of list.




So now the OP says the list will not always be in the same range but wants


the code to do as it does here as I have posted. The lists to processed


will be in different columns and not always starting at row 1.




I tried using 'For Each c In Selection' but I cannot figure out how to


identify the header in the Selection to copy across as needed.




Thanks.


Howard




Option Explicit




Sub cLant()




Dim c As Range


Dim i As Long


Dim rCt As Range




Set rCt = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)




i = 0


For Each c In rCt


c.Cut c.Offset(, i)


Range("A1").Copy Range("A1").Offset(, i)


i = i + 1


Next




End Sub


Thanks, Joeu,

Nice work! I believe all
assumptions are quite workable.

Thanks again and I will forward with a note of credit.

Regards,
Howard
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adjust Zip code to 9 Char fmt. Jeffery B Paarsa Excel Programming 11 May 28th 09 06:50 PM
help to adjust my code Anthony Excel Programming 2 March 14th 07 09:20 PM
Help to adjust code SiouxieQ Excel Programming 0 November 27th 04 07:45 PM
HELP - I need to adjust code!!!! jriendeau5[_3_] Excel Programming 1 November 5th 04 02:29 AM


All times are GMT +1. The time now is 10:31 PM.

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"