Range Object to set format ?

Muhammad Raza

New Member
Joined
Jun 11, 2014
Messages
4
Dear All

I am new to VBA and am trying to modify a VBA code which is below. The following code perfectly copies the values form one worksheet into another. But I also want it to copy formats.

Any ideas?? - Many Thanks

Sub Practice_1()


Dim datasource As Worksheet
Set datasource = ThisWorkbook.Worksheets("ABC2")
Dim tunnel As Range
Set tunnel = datasource.UsedRange
Dim WB As Workbook
Set WB = Workbooks.Add()
WB.Sheets(1).Range(tunnel.Address).Value = tunnel.Value


End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I mean i entered your line of code at the end and it didnt work.

If you wanted me to do something else - I told you I am new to VBA :)
 
Upvote 0
This will copy the UsedRange on worksheet ABC2 to the first sheet in a new workbook?

Code:
Sub Practice_2()
    Dim datasource As Worksheet
    Set datasource = ThisWorkbook.Worksheets("ABC2")
    Dim tunnel As Range
    Set tunnel = datasource.UsedRange
    Dim WB As Workbook
    Set WB = Workbooks.Add
    tunnel.Copy WB.Sheets(1).Range(tunnel.Address)
End Sub

Isn't that what you want?
 
Upvote 0
Thanks Andrew.. Working perfectly..

But I just realized if I use UsedRange - it will never hide the rows and columns that are hidden in worksheet ABC2 - This means that I will need to copy paste entire sheet to get the hidden property settings.. Am I correct ?
 
Upvote 0
Why don't you copy the worksheet rather than it's contents?

Code:
Sub CopySheet()
    ThisWorkbook.Worksheets("ABC2").Copy
End Sub

The new workbook will be the ActiveWorkbook.
 
Upvote 0

Forum statistics

Threads
1,216,171
Messages
6,129,284
Members
449,498
Latest member
Lee_ray

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