PC Review


Reply
Thread Tools Rate Thread

Copy & Paste from one Worksheet to another without giving focus?

 
 
Lars Uffmann
Guest
Posts: n/a
 
      22nd Jul 2008
My next problem:

I currently have this code to "delete" unwanted lines:
' **************************
Dim wb As Workbook ' wb for workBook
Dim ds As Worksheet ' ds for dataSheet

Set wb = Workbooks("myWorkbook")
Set ds = wb.Worksheets("myWorksheet")

ds.Columns.AutoFilter 4, "<>0" ' only show data where column 4 is <> 0
' (I want to "delete" where column 4 = 0)

ds.Rows.Copy ' copy into clipboard
' I'd prefer to give the new worksheet as destination
' and not use clipboard at all

ds.Name = "dummy" ' give worksheet a temporary name

' create new worksheet under old name
wb.Worksheets.Add().Name = "myWorksheet"

' select position for pasting
Worksheets("myWorksheet").Range("A1").Select

Selection.PasteSpecial ' paste data & formats

Application.DisplayAlerts = False
Worksheets("dummy").Delete ' delete old worksheet
Application.DisplayAlerts = True
' **************************

Now I would like to change the ds.Rows.Copy and Selection.PasteSpecial
to some code that also works in the background, if for example right
before Selection.PasteSpecial, someone clicks on a different worksheet -
I want to avoid pasting to that then. Any ideas?

Best Regards,

Lars
 
Reply With Quote
 
 
 
 
Lars Uffmann
Guest
Posts: n/a
 
      22nd Jul 2008
Found it:

' *******
Dim wb As Workbook
Dim oldWs As Worksheet, newWs As Worksheet

Set wb = Workbooks(filename & ".txt")
Set oldWs = wb.Worksheets("myWorksheet")

oldWs.Columns.AutoFilter 4, "<>0" ' only display where colum 4 <> 0
oldWs.Name = "dummy" ' rename old worksheet to temporary name

wb.Worksheets.Add().Name = "myWorksheet" ' create new worksheet
Set newWs = Worksheets("myWorksheet")
oldWs.Cells.Copy newWs.Cells ' keyline here, copy stuff over
newWs.Cells(1, 1).Select ' unselect everything

Application.DisplayAlerts = False
Worksheets("dummy").Delete ' delete old Worksheet
Application.DisplayAlerts = True

Set oldWs = Nothing
Set newWs = Nothing
' ********
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy & paste worksheet with print settings into new worksheet Larry Ohio Microsoft Excel Misc 1 23rd Dec 2009 05:17 PM
giving control focus = Focus? Select? ActiveControl? Activate? Zytan Microsoft VB .NET 9 19th Feb 2007 01:42 AM
Copy & paste cells fr open worksheet then close the worksheet =?Utf-8?B?U2lu?= Microsoft Excel Programming 1 2nd Oct 2006 02:20 PM
Re: excel 97: copy and paste values from one worksheet to another worksheet JMCN Microsoft Excel Programming 2 23rd Sep 2003 01:32 PM
excel 97: copy and paste values from one worksheet to another worksheet Kathy Microsoft Excel Programming 0 21st Sep 2003 03:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:14 AM.