Solved

MS Excel - Copy data range without existing formatting.

Posted on 2014-02-04
4
380 Views
Last Modified: 2014-02-04
In my vb.net application, I create a new worksheet in an existing Excel workbook by transferring data from an MS Access query.

With the following code (thanks again Harry Lee!), a range is selected in the new worksheet then copied to a different range in an old worksheet in the same workbook:

Dim NewWS As Worksheet, OldWS As Worksheet, NewRng As Range, OldLastRW As Long
NewWS = objExcel.Sheets("NewData")
OldWS = objExcel.Sheets("OldData")
OldLastRW = OldWS.Cells(OldWS.Rows.Count, 1).End(XlDirection.xlUp).Row
NewRng = NewWS.Range(NewWS.Cells(2, 1), NewWS.Cells(NewWS.Cells _(NewWS.Rows.Count, 1).End(XlDirection.xlUp).Row, 12))
NewRng.Copy(OldWS.Cells(OldLastRW + 2, 1))

All works well, but when the new worksheet was initially created from the Access query, the cells with data in them have borders which I do not want copied to the new worksheet.

What can I add/modify in the above code to prevent any formatting being copied with the data?
0
Comment
Question by:Tim313
4 Comments
 
LVL 15

Accepted Solution

by:
unknown_routine earned 500 total points
ID: 39833311
You can clear formats after copying:


For example

Range("C2:E4").Select
Selection.ClearFormats


So each time after you copy to destination, select the destination and then clear formats.
0
 
LVL 21

Expert Comment

by:Ejgil Hedegaard
ID: 39833412
If you have formulas to be copied with the range then the method from unknown_routine is the easiest way, but you say it is a table so probably you only need to copy values.

After copy (your last line) select the destination sheet and the upper left cell where you want the result, then insert the values with
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39833495
Although this question is asked in the excel zone, the more appropriate zone would be vb.net

I have not worked on vb.net but this works in VBA

OldWS.Cells(OldLastRW + 2, 1).resize(NewRng.rows,NewRng.columns).value=NewRng.value
0
 

Author Closing Comment

by:Tim313
ID: 39834296
To all, thanks for your suggestions.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now