VBA to copy range data with all visible and invisible cells to another sheet

PritishS

Board Regular
Joined
Dec 29, 2015
Messages
119
Office Version
  1. 2007
Platform
  1. Windows
Dear Sirs,
Wish you all a Happy New Year!

After doing 2 days of searching I'm here for you kind help!

Requirement:

I want to copy a row range of data to other worksheet. For example- Range(A1:H1) from sheet1 to sheet2. Im am using this code below to do this
Code:
Sub CopyData()
Dim nextrow As Long, rngRow As Range, rfound As Range, sFind As String, Worksheet As Worksheet
nextrow = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row + 1

Set rngRow = Range("A" & ActiveCell.Row & ":H" & ActiveCell.Row)
rngRow.Copy Destination:=Sheets("Sheet2").Range("A" & nextrow)

sFind = Range("A" & ActiveCell.Row).Value
             
With Sheets("Sheet2")
 Set rfound = .Columns(1).Find(What:=sFind, After:=.Cells(1, 1))
End With

Set rngRow = Nothing

End Sub

Problem Facing:
My problem is in this range A:H, D and F columns are hidden.
Sheet1:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A1[/TD]
[TD]B1[/TD]
[TD]C1[/TD]
[TD]D1[/TD]
[TD]E1[/TD]
[TD]F1[/TD]
[TD]G1[/TD]
[TD]H1[/TD]
[/TR]
[TR]
[TD]Pen[/TD]
[TD]Pencil[/TD]
[TD]Inkpot[/TD]
[TD][/TD]
[TD]Ball[/TD]
[TD][/TD]
[TD]Bat[/TD]
[TD]Hat[/TD]
[/TR]
</tbody>[/TABLE]

With above mentioned code after pasting the same range in sheet2 is giving me result as shown below
Sheet2:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A1[/TD]
[TD]B1[/TD]
[TD]C1[/TD]
[TD]D1[/TD]
[TD]E1[/TD]
[TD]F1[/TD]
[TD]G1[/TD]
[TD]H1[/TD]
[/TR]
[TR]
[TD]Pen[/TD]
[TD]Pencil[/TD]
[TD]Inkpot[/TD]
[TD]Ball[/TD]
[TD]Bat[/TD]
[TD]Hat[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

But requirement is

Sheet2:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A1[/TD]
[TD]B1[/TD]
[TD]C1[/TD]
[TD]D1[/TD]
[TD]E1[/TD]
[TD]F1[/TD]
[TD]G1[/TD]
[TD]H1[/TD]
[/TR]
[TR]
[TD]Pen[/TD]
[TD]Pencil[/TD]
[TD]Inkpot[/TD]
[TD][/TD]
[TD]Bal[/TD]
[TD][/TD]
[TD]Bat[/TD]
[TD]Hat[/TD]
[/TR]
</tbody>[/TABLE]

Note:

Please note I can not unhide those cells, copy and paste then hide those cell. Because in actual case I have lots of column from A to CA and many are hidden columns and those are not fixed. So vba to unhide columns, copy paste and then hide is not applicable in this case.

Questions:

Is there any direct vba code like (xlcelltypevisible) which can be used to get the full range with hidden cells?

Thank you Very much in advance!!

Thanks & Regrads,
PritishS
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
If you just want the visible cells copied try
Code:
Sub CopyData()
   Dim nextrow As Long, rngRow As Range, rfound As Range, sFind As String, Worksheet As Worksheet
   Dim rng As Range
   nextrow = Sheets("New").Cells(Rows.Count, 1).End(xlUp).Row + 1
   
   Set rngRow = Range("A" & ActiveCell.Row & ":H" & ActiveCell.Row)
   For Each rng In rngRow.SpecialCells(xlVisible).Areas
      rng.Copy Sheets("New").Cells(nextrow, rng.Column)
   Next rng
   
   sFind = Range("A" & ActiveCell.Row).Value
                
   With Sheets("New")
    Set rfound = .Columns(1).Find(What:=sFind, After:=.Cells(1, 1))
   End With
   
   Set rngRow = Nothing

End Sub
 
Upvote 0
"The world is Full with Good people and I always find them everywhere whenever in need!!".

Thank you Fluff Sir, Thank you very much. This code works exactly how I wanted. and moreover I learnt a new thing!!
Many you very much for saving my day!
Wish you a Happy and prosperous New Year!!

Thanks & Regards,
PritishS
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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