Pasting Into Visible Cells Only

ModerCreed

New Member
Joined
May 28, 2012
Messages
20
Hey guys,

I am new here and have been searching around the internet for a solution to this all morning and although people have asked this question none of the solutions are working for me.

I am using excel 2007 on a 64 bit windows 7 machine if that matters.

Anyways, I am trying to copy a range of cells from one workbook and then paste that range into another workbook, but ONLY on the visable rows in that notebook.

The first workbook has no hidden rows so I don't need to do any go-to specials to copy them, but the second one of course does have them.

It seems I am going to need some sort of macro to do this, but I am not super experienced yet with how to make them. Is is possible that someone could provide me guidance here?

I will rephrase my question another way in case it's confusing:

This is a simplified example.

- Workbook 1 has column A with cells 1-10 with data in them that are going to be copied

- Workbook 2 also has a column A, but it has information in cells 1-15 with cells 6-10 hidden from view.

- The copied data from workbook one needs to only land on the visible cells (read: cells 1-5 and cells 10-15)

Thanks for any help in advance!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi ModerCreed

I created this code which (seems to) work although I suspect there's a hotter solution out there. First select the source range and run the SpecialCopy macro, then select the destination range (you need to select the whole range not just the upper-left cell) and run SpecialPaste.

Code:
Option Explicit

Dim CopyRange As Range
     Public Sub SpecialCopy()
Set CopyRange = Selection

End Sub

Public Sub SpecialPaste()

If CopyRange Is Nothing Then
    MsgBox "Nothing to paste"
    Exit Sub
End If
If CopyRange.Columns.Count <> Selection.Columns.Count Then
    MsgBox "Different number of columns selected"
    Exit Sub
End If

Dim DestRange As Range
Set DestRange = Selection

Dim i As Integer, r As Integer, x As Integer
r = 1

For i = 1 To DestRange.Rows.Count
    If DestRange.Rows(i).Height > 0 Then
        For x = 1 To CopyRange.Columns.Count
            DestRange.Cells(i, x).Value = CopyRange.Cells(r, x).Value
        Next x
        r = r + 1
    End If
    If r > CopyRange.Rows.Count Then Exit Sub
Next i

End Sub

Here's the output I got:

Excel 2010
AB
1Source_c_1Source_c_2
21A
32B
43C
54D
65E
76F
87G
98H
109I
1110J
12
13
14
15Dest_c_1Dest_c_2
161A
172B
18HIDDENHIDDEN
19HIDDENHIDDEN
20HIDDENHIDDEN
213C
22HIDDENHIDDEN
234D
245E
25HIDDENHIDDEN
26HIDDENHIDDEN
276F
287G
298H
309I
3110J
Sheet1

Let me know if you have any questions.

Regards
Adam
 
Upvote 0
No problem. Glad it did what you needed; and thanks for the feedback.

Regards
Adam
 
Upvote 0
Spent ages to try and find a solution to paste range only into visible cells/rows and your macro was the only thing that managed to work. Thanks very much!
 
Upvote 0
This is exactly what I need but I am a newbie. Is there anyone out there kind enough and patient enough to step me through how this works? I copied the code into a module. Now what?
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top