Solved

MS Excel - Copy data range without existing formatting.

Posted on 2014-02-04
4
378 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
To all, thanks for your suggestions.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel - Row Height +1 VBA 2 23
Merging multiple rows to one 22 33
Convert .PDF 6 38
VLOOKUP Function MS Excel 2010 2 20
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

744 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

19 Experts available now in Live!

Get 1:1 Help Now