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:
A1B1C1D1E1F1G1H1
PenPencilInkpotBallBatHat

<tbody>
</tbody>

With above mentioned code after pasting the same range in sheet2 is giving me result as shown below
Sheet2:
A1B1C1D1E1F1G1H1
PenPencilInkpotBallBatHat

<tbody>
</tbody>

But requirement is

Sheet2:
A1B1C1D1E1F1G1H1
PenPencilInkpotBalBatHat

<tbody>
</tbody>

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

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
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,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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