PC Review


Reply
Thread Tools Rate Thread

Create Custom Paste Special - Row Heights

 
 
meh2030@gmail.com
Guest
Posts: n/a
 
      25th Oct 2007
My code thus far is below.

Does anyone know how Microsoft programmatically performs paste special
in Excel? The copy, paste special - column widths is useful, but I
want to perform a paste special - row heights. Below I have included
some syntax for this, but there is one problem - accessing the CutCopy
range.

I have done some searching and have found individuals who have said
that is not possible (posts from 2004); however, there must be some
way to do this, otherwise paste special would not exist at all. This
being said, my hang up is on the syntax line below that states "Set
copyRng = Selection ..." I want the "Selection" to be the CutCopy
range.

So, one idea that I have is to see if there is a way to determine
which sheet has the CutCopyMode activated. (I know that
Application.CutCopyMode will identify from an application point of
view, but I'm wondering if this can be determined from a worksheet
level).

Additionally, I found the code below posted by Bob Phillips; however,
I want to place this row heights macro in my personal.xls file so that
I can use it on any workbook.

Public oldCell As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not oldCell Is Nothing Then MsgBox oldCell.Address
Set oldCell = Target
End Sub

Any ideas would be greatly appreciated. Thanks in advance.

Sub paste_Row_Heights()

'This sub procedure is intended to act as a copy, paste special
'wherein the row heights are "pasted". Excel has a copy, paste
'special - column widths, but no paste special - row heights.

Dim copyRng As Range
Dim rowRng As Range
Dim numRows As Long
Dim rowHgt() As Double
Dim rowCell As Range
Dim i As Long
Dim j As Long
Dim a

'need to have "copied" a range in order to "paste" the row heights
'at a new location
If Application.CutCopyMode = False Then Exit Sub

'create a range object that storse the range that was copied
'(i.e. the one with the cut/copy border)

Set copyRng = Selection 'want "Selection" to be the "copied" range
'maybe use a sheet selection change/deactivate
event,
'but that doesn't cure a same sheet pasting
Debug.Print copyRng.Address

numRows = copyRng.Rows.Count 'copy the number of rows in the copied
range

'create a new range object that contains only 1 column

Set rowRng = copyRng.Range(Cells(1, 1), Cells(numRows, 1))

ReDim rowHgt(1 To numRows)

'store the row heights of the copied range

i = 0
For Each rowCell In rowRng.Cells
i = i + 1
rowHgt(i) = rowCell.RowHeight
Debug.Print i; ":"; rowHgt(i)
Next

'"paste" the row heights where the active cell is

For j = LBound(rowHgt) To UBound(rowHgt)
Selection.Cells(1, 1).Offset(j - 1, 1).RowHeight = rowHgt(j)
Next

'need to build an error handler to handle a situation where you
'have 10 rows copied and your paste cell is on row 65,530 and
'there are only 7 row location that can be "pasted"
End Sub

 
Reply With Quote
 
 
 
 
Doug Glancy
Guest
Posts: n/a
 
      25th Oct 2007
Hi there,

My thought is to paste formats to a new, temporary worksheet, count the rows
and save their heights as per your code, then delete the temporary worksheet
and restore the ActiveSheet and Selection and then apply the row heights per
your code. I didn't test it much but it works pasting from one sheet to
another:

Dim wsActiveSheet As Worksheet
Dim rngActiveCell As Range
Dim wsTempSheet As Worksheet
Dim copyRng As Range
Dim rowRng As Range
Dim numRows As Long
Dim rowHgt() As Double
Dim rowCell As Range
Dim i As Long
Dim j As Long
Dim a

Application.ScreenUpdating = False

Set wsActiveSheet = ActiveSheet
Set rngActiveCell = Selection
'need to have "copied" a range in order to "paste" the row heights
'at a new location
If Application.CutCopyMode = False Then Exit Sub

'create a range object that storse the range that was copied
'(i.e. the one with the cut/copy border)

ThisWorkbook.Worksheets.Add
Set wsTempSheet = ActiveSheet
wsTempSheet.Range("A1").PasteSpecial (xlPasteFormats)

Set copyRng = Selection 'want "Selection" to be the "copied" range
'maybe use a sheet selection change/deactivate
event,
'but that doesn't cure a same sheet pasting
Debug.Print copyRng.Address

numRows = copyRng.Rows.Count 'copy the number of rows in the copied Range

'create a new range object that contains only 1 column

Set rowRng = copyRng.Range(Cells(1, 1), Cells(numRows, 1))

ReDim rowHgt(1 To numRows)

'store the row heights of the copied range

i = 0
For Each rowCell In rowRng.Cells
i = i + 1
rowHgt(i) = rowCell.RowHeight
Debug.Print i; ":"; rowHgt(i)
Next

Application.DisplayAlerts = False
wsTempSheet.Delete
Application.DisplayAlerts = True

'"paste" the row heights where the active cell is
wsActiveSheet.Activate
rngActiveCell.Select
For j = LBound(rowHgt) To UBound(rowHgt)
Selection.Cells(1, 1).Offset(j - 1, 1).RowHeight = rowHgt(j)
Next

'need to build an error handler to handle a situation where you
'have 10 rows copied and your paste cell is on row 65,530 and
'there are only 7 row location that can be "pasted"

Application.ScreenUpdating = True

End Sub

hth,

Doug
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> My code thus far is below.
>
> Does anyone know how Microsoft programmatically performs paste special
> in Excel? The copy, paste special - column widths is useful, but I
> want to perform a paste special - row heights. Below I have included
> some syntax for this, but there is one problem - accessing the CutCopy
> range.
>
> I have done some searching and have found individuals who have said
> that is not possible (posts from 2004); however, there must be some
> way to do this, otherwise paste special would not exist at all. This
> being said, my hang up is on the syntax line below that states "Set
> copyRng = Selection ..." I want the "Selection" to be the CutCopy
> range.
>
> So, one idea that I have is to see if there is a way to determine
> which sheet has the CutCopyMode activated. (I know that
> Application.CutCopyMode will identify from an application point of
> view, but I'm wondering if this can be determined from a worksheet
> level).
>
> Additionally, I found the code below posted by Bob Phillips; however,
> I want to place this row heights macro in my personal.xls file so that
> I can use it on any workbook.
>
> Public oldCell As Range
> Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> If Not oldCell Is Nothing Then MsgBox oldCell.Address
> Set oldCell = Target
> End Sub
>
> Any ideas would be greatly appreciated. Thanks in advance.
>
> Sub paste_Row_Heights()
>
> 'This sub procedure is intended to act as a copy, paste special
> 'wherein the row heights are "pasted". Excel has a copy, paste
> 'special - column widths, but no paste special - row heights.
>
> Dim copyRng As Range
> Dim rowRng As Range
> Dim numRows As Long
> Dim rowHgt() As Double
> Dim rowCell As Range
> Dim i As Long
> Dim j As Long
> Dim a
>
> 'need to have "copied" a range in order to "paste" the row heights
> 'at a new location
> If Application.CutCopyMode = False Then Exit Sub
>
> 'create a range object that storse the range that was copied
> '(i.e. the one with the cut/copy border)
>
> Set copyRng = Selection 'want "Selection" to be the "copied" range
> 'maybe use a sheet selection change/deactivate
> event,
> 'but that doesn't cure a same sheet pasting
> Debug.Print copyRng.Address
>
> numRows = copyRng.Rows.Count 'copy the number of rows in the copied
> range
>
> 'create a new range object that contains only 1 column
>
> Set rowRng = copyRng.Range(Cells(1, 1), Cells(numRows, 1))
>
> ReDim rowHgt(1 To numRows)
>
> 'store the row heights of the copied range
>
> i = 0
> For Each rowCell In rowRng.Cells
> i = i + 1
> rowHgt(i) = rowCell.RowHeight
> Debug.Print i; ":"; rowHgt(i)
> Next
>
> '"paste" the row heights where the active cell is
>
> For j = LBound(rowHgt) To UBound(rowHgt)
> Selection.Cells(1, 1).Offset(j - 1, 1).RowHeight = rowHgt(j)
> Next
>
> 'need to build an error handler to handle a situation where you
> 'have 10 rows copied and your paste cell is on row 65,530 and
> 'there are only 7 row location that can be "pasted"
> End Sub
>


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create a button for Edit/paste Special/Unformatted text ? dan_pub Microsoft Outlook Discussion 2 30th Mar 2009 06:46 AM
how to create a copy/paste special/transpose macro? gerry.lisa@gmail.com Microsoft Excel Misc 6 8th Aug 2007 06:07 AM
Create new sheet from cell-info and then copy/paste special Ingve Microsoft Excel Programming 0 19th Jan 2006 10:58 PM
Macro to Paste Special Row Heights Hash@example.org Microsoft Excel Programming 0 23rd Mar 2005 12:38 AM
Dynamic Copy/Paste Special Formulas/Paste Special Values Sharon Perez Microsoft Excel Programming 3 7th Aug 2004 09:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:40 AM.