View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Tom Hutchins Tom Hutchins is offline
external usenet poster
 
Posts: 1,069
Default Paste To Visible Cells Macro

The following macro is not quite you described. I'm losing the contents of
the clipboard when I invoke the macro - not sure why. This macro copies a
selected cell to only the visible cells in a selected range, but you run it
with the cell selected that you want to copy. The macro prompts you to select
the range to which it should be pasted, then copies & pastes the starting
cell to the visible cells in the range.

Public Sub CopyToVisibleOnly()
'Declare local variables.
Dim cl As Range, Target As Range
Dim CopyRng As Range, ret
'Start with cell selected that you want to copy.
Set CopyRng = Selection
'Now select the range where it should be pasted.
Set Target = Application.InputBox _
(Prompt:="Select the Paste range", Type:=8)
For Each cl In Target
'Only cells in visible rows in the selected
'range are pasted.
If cl.EntireRow.Hidden = False Then
CopyRng.Copy
cl.Select
ActiveSheet.Paste
End If
Next cl
'Free the object variables.
Set CopyRng = Nothing
Set Target = Nothing
End Sub

I'll keep working on a solution that more closely meets your request.

Hope this helps,

Hutch

"WBTKbeezy" wrote:

Hello:

I was trying to find a way to paste into visible cells only. I found a macro
on another site and have pared it down to the following:

Sub VisbleCellsPasting()
On Error Resume Next

Dim aRng As Range
Dim aRn As Range
Dim bRng As Range

Set aRng = ActiveSheet.Range("B234:B273")

Range("F2").Select

For Each aRn In aRng
Set bRng = ActiveCell
If bRng.EntireRow.Hidden = False Then
bRng = aRn
bRng.Offset(1).Select
Else
Do
If ActiveCell.EntireRow.Hidden = True Then
ActiveCell.Offset(1).Select
End If
Loop Until ActiveCell.EntireRow.Hidden = False
ActiveCell = aRn
ActiveCell.Offset(1).Select
End If

Next

End Sub

This code works fine, but I have some issues...
The macro hard codes the selection that I need to paste and where I need to
paste it. I would like to be able to use the normal Excel copy (Ctrl+C) on my
data I need to paste (where ever it lives whether in a new workbook or not)
and click the macro button and have it paste where I need it. Is this
possible?