Solved

MS Excel - Copy data range without existing formatting.

Posted on 2014-02-04
4
385 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 22

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

734 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